Using curl and PHP to talk to a REST service

Having recently written articles about curl and about writing a PHP REST server, I thought I'd complete the circle and put here a few notes on using PHP's curl wrapper as a Rest client. Also I had to look some of this up when I needed to actually do it so next time I need only look in one place!

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" => 'lorna@example.com',
            );
       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!

14 thoughts on “Using curl and PHP to talk to a REST service

  1. 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.

  2. 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...

  3. 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

  4. 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!

  5. Pingback: Development Note for My UW Lib Project (1) | Blog of Yifei Zhao beta

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>