SimonHuber
asked on
PHP RESTful Request
Hi Experts,
I need some help making RESTful GET request in PHP.
I have an application that I want to get information from, the data is returned as XML. I can get the data using the WizTools REST Client 3.0.
The parameters I set in the tool are as follows.
Method: GET
Header: application/xml
Auth: BASIC
Etc.: HTTP Version 1.1, Follow HTTP Redirects = TRUE.
URL: http://website.com:8080/<rest query>
I have been looking at various web tutorials and the cURL manual but to no avail. I need to use cURL as latter on I want to also send POST requests.
Thanks for looking at this question.
Simon
I need some help making RESTful GET request in PHP.
I have an application that I want to get information from, the data is returned as XML. I can get the data using the WizTools REST Client 3.0.
The parameters I set in the tool are as follows.
Method: GET
Header: application/xml
Auth: BASIC
Etc.: HTTP Version 1.1, Follow HTTP Redirects = TRUE.
URL: http://website.com:8080/<rest query>
I have been looking at various web tutorials and the cURL manual but to no avail. I need to use cURL as latter on I want to also send POST requests.
Thanks for looking at this question.
Simon
Is this useful?
<?php
function getXML($xml, $url, $timeout=30, $encode=true)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_NOSIGNAL, 1);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
// When you want to post
// curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$http = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
$xml = new SimpleXMLElement(html_entity_decode($http));
return $xml;
}
$xml = <<<XML
<request>
<type>timesfive</type>
<value>10</value>
</request>
XML;
$xml = getXml($xml, 'http://server/getxml.php');
echo $xml->result;
?>
ASKER
Hi Ray,
Thanks for the response, I haven;t got time to try it right now but will do tomorrow.
Am not sure though that's exactly what I am looking for since there is no mention of setting up the user name and password for the request.
The application I am using is not publicly available so there would be no way of you testing it.
Thanks again.
Simon
Thanks for the response, I haven;t got time to try it right now but will do tomorrow.
Am not sure though that's exactly what I am looking for since there is no mention of setting up the user name and password for the request.
The application I am using is not publicly available so there would be no way of you testing it.
Thanks again.
Simon
In a RESTful interface, you make an atomic request. The arguments usually go into the URL and since this is a GET method request, it might look like this:
http://url?u=USER&p=PASS
http://url?u=USER&p=PASS
The URL might also look like this for HTTP authentication. We would really need to see the API documentation to know how to give you more guidance.
https://USER:PASS@url?q=QUERY
See also: http://en.wikipedia.org/wiki/Representational_state_transfer
https://USER:PASS@url?q=QUERY
See also: http://en.wikipedia.org/wiki/Representational_state_transfer
ASKER
Hi,
I managed to track down the issue to a DNS issue between my web server and application server by using the curl -v command at the command line.
So now I have a working connection between my web server and application server using the following code.
This produces the following result.
So my question now is how I access these elements. Looking around on the web it would seem most people are suggestion a "foreach" loop. However I cannot get this to produce any results. Ideally I would need to assign these elements to variables for future use and in all likely hood not print these out in the web browser.
Many thanks.
I managed to track down the issue to a DNS issue between my web server and application server by using the curl -v command at the command line.
So now I have a working connection between my web server and application server using the following code.
<?php
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_URL => 'http://10.10.10.10/<URI>',
CURLOPT_PORT => '8080',
CURLOPT_USERPWD => '<USER>:<PASSWORD>',
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_RETURNTRANSFER => true,
//CURLOPT_HEADER => true
)
);
$response = curl_exec($ch);
curl_close($ch);
echo '<pre>';
$xml = new SimpleXMLElement($response);
print_r($xml);
?>
This produces the following result.
SimpleXMLElement Object
(
[@attributes] => Array
(
[total-servers] => 1
)
[server] => SimpleXMLElement Object
(
[id] => 0x100000
[name] => server-1
[isPrimary] => true
[Version] => 9.0
)
)
So my question now is how I access these elements. Looking around on the web it would seem most people are suggestion a "foreach" loop. However I cannot get this to produce any results. Ideally I would need to assign these elements to variables for future use and in all likely hood not print these out in the web browser.
Many thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
See http://www.laprbass.com/RAY_curl_get_example.php
Open in new window
HTH, ~Ray