Saturday, July 4, 2009

Simple PHP Rest Client using cURL

I recently worked with a third party API that uses REST. I've been reading about REST all this time and this is the first time I've actually used it. Surprised me because it was really nice and simple. It's a lot easier trying it out than reading about it (but it sure helps to read some about it first :). What took quite some time was finding a REST client that I will use to connect and some authorization issues (which is site specific).

If you're already using Zend Framework, you might want to try out it's built in rest client. But if you're looking for a simpler solution, you can do well with PHP's cURL functions. Honestly I don't like cURL because it really looks dirty when you use it (aesthetically that is), and all the options you need to set kinda bums me. It's like you have to research a lot of option flags just to make something work. But hey, it almost always gets the job done :) Good thing iContact's API documentation (the API I'm working with) has a downloadable sample code that uses plain cURL functions to connect to it's REST service. I have to credit them for most of the code. I just did some minor edits so it is more generalized. So here, try it out if it works for you (this is plain PHP code, hopefully you'll put this in a function or method :)

This example is specific to JSON input/output, so you have to change some stuff if the API requires XML or some other format.

$url = 'https://www.google.com';
$method = 'POST';

# headers and data (this is API dependent, some uses XML)
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
);
$data = json_encode(array(
'firstName'=> 'John',
'lastName'=> 'Doe'
));

$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);

switch($method)
{

case 'GET':
break;

case 'POST':
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
break;

case 'PUT':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
break;

case 'DELETE':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
}

$response = curl_exec($handle);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);

16 comments:

  1. Just what I needed. Also, you did a nice job of putting into words my own uneasiness with cURL.

    ReplyDelete
  2. hey great one ... I tried several script but the only one that actually worked with authentication stuff ! Thanks a lot

    ReplyDelete
  3. Hello Antonio, i tried to use your script but didnt work. There something else to change or its just run it ?
    Thanks!

    ReplyDelete
  4. I found this very nice. However, I am stuck on PUT and DELETE. How am I suppose to get these on the server side?

    ReplyDelete
  5. How to delete & Upadate.. pls help me out
    mail me vishves@gmail.com its urgent

    ReplyDelete
  6. thanxs man.job saver

    ReplyDelete
  7. Wow. Seriously, thank you. Didn't see

    curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);

    anywhere, you saved my ass

    ReplyDelete
  8. How is the $data passed when its a GET request?

    ReplyDelete
  9. https://github.com/educoder/pest

    ReplyDelete
  10. Excelente!!! 3 dias buscando esto!

    ReplyDelete
  11. There is another curl client https://github.com/ARACOOOL/dHttp

    ReplyDelete
  12. I want GET method code for REST Client using Authentication.

    ReplyDelete