Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 677
  • Last Modified:

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
0
SimonHuber
Asked:
SimonHuber
  • 3
  • 2
  • 2
1 Solution
 
Ray PaseurCommented:
Here is a CURL GET request script.  If you want to post the URL of the site's API, I'll be glad to show you how to read and interpret the data from the API.

See http://www.laprbass.com/RAY_curl_get_example.php

<?php // RAY_curl_get_example.php
error_reporting(E_ALL);

// DEMONSTRATE THE BASICS OF CURL

// SINCE IT IS ON MY SERVER, I HAVE HARD-CODED THIS URL
$url = 'https://twitter.com/RayPaseur';

// TRY THE REMOTE WEB SERVICE
$htm = my_curl($url);

// SHOW THE WORK PRODUCT OR BARK OUT ERROR MESSAGES
echo "<pre>";
echo PHP_EOL . '<strong>' . $url . '</strong>';
echo PHP_EOL . htmlentities($htm);
echo PHP_EOL;


// A FUNCTION TO RUN A CURL-GET CLIENT CALL TO A FOREIGN SERVER
function my_curl
( $url
, $timeout=3
, $error_report=TRUE
)
{
    $curl = curl_init();

    // HEADERS AND OPTIONS APPEAR TO BE A FIREFOX BROWSER REFERRED BY GOOGLE
    $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: "; // BROWSERS USUALLY LEAVE THIS BLANK

    // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
    curl_setopt( $curl, CURLOPT_URL,            $url  );
    curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'  ); // ANCIENT HISTORY
    curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1'  );
    curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header  );
    curl_setopt( $curl, CURLOPT_REFERER,        'http://www.google.com'  );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );
    curl_setopt( $curl, CURLOPT_AUTOREFERER,    TRUE  );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE  );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE  );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout  );

    // RUN THE CURL REQUEST AND GET THE RESULTS
    $htm = curl_exec($curl);

    // ON FAILURE HANDLE ERROR MESSAGE
    if ($htm === FALSE)
    {
        if ($error_report)
        {
            $err = curl_errno($curl);
            $inf = curl_getinfo($curl);
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            var_dump($inf);
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS RETURN XML / HTML STRING
    curl_close($curl);
    return $htm;
}

Open in new window

HTH, ~Ray
0
 
Julian HansenCommented:
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;
?>

Open in new window

0
 
SimonHuberAuthor Commented:
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
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
0
 
Ray PaseurCommented:
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
0
 
SimonHuberAuthor Commented:
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.

<?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);
?>

Open in new window


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
        )

)

Open in new window


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.
0
 
Julian HansenCommented:
You can access like this

(int)$xml->server->id
(string)$xml->server->name
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now