Using curl and PHP to talk to a REST service
If you don’t know about PHP, Rest, or curl, then I recommend you do a little reading around each of those subjects before reading this as its unlikely to make much sense – I’m not including background on these topics as there are better resources elsewhere.
I’ve written about using curl before from the command line, but this example uses PHP’s curl to access the service. This is just a simple example, but hopefully if you are doing something along these lines you can adapt for your needs.
In the example, we set the URL we’d like to call and initialise the curl object to point to that. Then we create an array of post data, and configure curl to use POST to make the request and to use our data array.
$service_url = 'http://example.com/rest/user/';
$curl = curl_init($service_url);
$curl_post_data = array(
"user_id" => 42,
"emailaddress" => '[email protected]',
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);
curl_close($curl);
$xml = new SimpleXMLElement($curl_response);
We execute the request and capture the response. Health warning: by default curl will echo the response (for reasons that aren’t clear to me), if you want to parse it then you will need to use the CURLOPT_RETURNTRANSFER flag to have curl return the response rather than a boolean indication of success – this fooled me completely for a while, I have no idea why it works this way. As you can see I’ve parsed the resulting XML and my script can then continue with the data it acquired. Depending what you need to do next, you can manipulate the SimpleXMLElement object as you need to.
If you’re working with PHP and services, then hopefully this will get you started, if you have any questions or comments, then please add them below!
Good post Lorna!
Lineke also has a blog post on multithreading in PHP with the curl extension which might be useful to people who want to build a high performance layer on top of a REST service: http://www.ibuildings.com/blog/archives/811-Multithreading-in-PHP-with-CURL.html.
One day I should write a blog post about how I used that…
Boy: Thanks for dropping by and pointing at Lineke’s post, that’s a great addition (and I hadn’t seen it). You should definitely blog about your usage of that, I’d be really interested.
Thanks Lorna, this article is very useful!
Great post as always Lorna! That said, I *hate* cURL. :P I think it’s a great way for people still running PHP pre-5 to get things done.
For 5 and on and for what you’re using in this particular example, the HTTP streams wrapper. Check out Example #1 here: http://php.net/manual/en/context.http.php.
If you need all the functionality that cURL provides with a bit less code, pecl_http is great: http://pecl.php.net/package/pecl_http.
Robert: Glad to help!
Matthew: How funny, I absolutely love curl, its a clean protocol and widely used. That said, I think I need to come to terms with my own command-line-geekery some day soon…
Thanks for this very simple, straightforward and easy-to-follow sample plus explanation.
Just what the doctor ordered! Please, keep up the good work.
WebGyver: Thanks for dropping in and for your kind words :)
beside REST webservice, cURL also can connect to others like XMLRPC or JSON.
cURL can simulate web browser, that’s make cURL is coold.
Thanks for the article I was debugging my script for 3 hrs and could find out response was “1” not the expected data
Very nice article :) It helps me a lot!
Keep up the good work!
Greets from Germany!
this is doing my head in…. I’ve been trying for hours…
I’m using the your code to try to post a record. this si the data strucutre:
being using this code and I cat get it working… please help ! :( Just say i only want the below fields populated
$fileds = array(
‘insertContact’ => array(
‘contact’ => array(
‘givenName’ => urlencode(‘John’),
‘familyName’ => urlencode(‘Smith’),
‘organisationName’ => urlencode(‘Saasy.tv’)
)
)
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);
curl_close($curl);
$xml = new SimpleXMLElement($curl_response);
service_url
Make sure you have php5-curl installed:
apt-get install php5-curl
I removed the bulk of the data you posted as I wasn’t sure it was helpful and it seems silly to post the data into a public forum. My first questions would be: 1) where is curl_post_data set? 3) what do you get in $curl_response before you try to transform it to XML? I suspect you need to set the post data and encode in in PHP, depending which service you’re sending it to. I hope that helps, at least a bit … good luck!
Pingback: Development Note for My UW Lib Project (1) | Blog of Yifei Zhao beta
nice…thnx for the example
Pingback: How to access RESTful API via PHP | WordPress - Start-Up
Good post lorna. It worked like a charm
when i am using post method(curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);) it give me result
stdClass Object
(
[id] => 501
) not a whole array
this is my api url
Check the headers that arrive with that response, or sounds like the API is returning you an error
This still works wonders!
Awesome. Found this and worked like charm