Accessing a REST API with PHP I'm currently using a scientific information management system that employs a RESTful API. I've never used this API with PHP, and I had a hard time finding best practice examples online. After some reading and experimenting, I came up with the following proof-of-concept procedure to retrieve data from the API and then upload the XML object, slightly modified, in order to change information in the system.

I'm most concerned with PHP best practices here: is my PHP efficient, robust, and secure? Is there any code I can remove or make more efficient? I'm using PHP v5.3.8

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;?php
$uriPrefix = 'http://[user:pass]@[path_to_web_app:port]/api/v2/';

//Open up a connection to the API
//for the container:
$containerSuffix = 'containers/27-95';
$containerUri = $uriPrefix . $containerSuffix;
$containerCurl = curl_init($containerUri);

//for the sample:
$sampleSuffix = 'samples/DOR6A12';
$sampleUri = $uriPrefix . $sampleSuffix;
$sampleCurl = curl_init($sampleUri);

//API-specific settings
curl_setopt($containerCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($sampleCurl, CURLOPT_RETURNTRANSFER, true);

//get the yummy data
$containerResult = curl_exec($containerCurl);
$containerXml = new SimpleXMLElement($containerResult);
$sampleResult = curl_exec($sampleCurl);
$sampleXml = new SimpleXMLElement($sampleResult);

//close the API connection
curl_close($containerCurl);
curl_close($sampleCurl);

echo '&lt;h2&gt;Some XML for a LIMS Container:&lt;/h2&gt;';
echo htmlentities($containerXml-&gt;asXML());
echo '&lt;h2&gt;Some XML for a LIMS Sample:&lt;/h2&gt;';
echo htmlentities($sampleXml-&gt;asXML());

//Now let's infer the project ID for the sample from the XML result
$limsId = $sampleXml-&gt;project['limsid'];
echo "&lt;p&gt;The project ID for the sample is $limsId&lt;/p&gt;";

//Now let's change the sample name
$newName = 'New Sample Name';
$oldName = (string)$sampleXml-&gt;name;
$sampleXml-&gt;name = $newName;

$changeSampleCurl = curl_init($sampleUri);
curl_setopt($changeSampleCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($changeSampleCurl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($changeSampleCurl, CURLOPT_POSTFIELDS,$sampleXml-&gt;asXML());

$changeSampleResult = curl_exec($changeSampleCurl);
echo '&lt;h2&gt;The submitted XML&lt;/h2&gt;';
echo htmlentities($changeSampleResult);

curl_close($changeSampleCurl);

echo "&lt;p&gt;The old sample name was $oldName&lt;/p&gt;";
echo "&lt;p&gt;It should now be $newName&lt;/p&gt;";
?&gt;

&lt;/body&gt;
&lt;/html&gt;
The most obvious omission is error handling. You don't handle curl errors or xml parsing errors.

A REST api is like any other external service you might use in your application (such as a database). You might consider creating a client library for it (first check to see if one does not already exist for the service in question). Example usage with the implementation details neatly tucked away:

&lt;?php
//read these values from your configuration and set here
$connection = array(
    'user' =&gt; '',
    'pass' =&gt; '',
    'baseUrl' =&gt; '',
    'version' =&gt; ''
);
try {
    $client = new FooClient($connection);
    $container = $client-&gt;getContainer('27-95');
    if ($container) {
        //do something with the fetched container
    }

    $sample = $client-&gt;getSample('DOR6A12');
    if ($sample) {
        $sample-&gt;setName('new name');
        $client-&gt;saveSample($sample);
    }
} catch (FooClientException $e) {
    //handle accordingly
}


I'm assuming that the client will return mapped objects for the getContainer and getSample methods. These mapped objects would be instances of php classes which are part of this client library. The xml responses returned by the service would be mapped to to these returned objects. That way, you are not directly dealing with the xml here, rather those details are dealt with in your client library (errors are handled there too with an appropriate exception being thrown if need be).