Handling responses from PHP’s cURL

Posted by 6bytes at 14 April 2010

Category: PHP

Tags: , ,

Recently I had a small problem with cURL. The request was supposed to return only JSON data to use in my app. I wrote the following:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://somedomain.com/api/123');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_exec($curl);
$status = curl_getinfo($curl);
curl_close($curl);

The variable $status contained the API’s response in plain text and concatenated, inaccessible in this format, array with cURL’s status response. This made impossible for example checking if the request was successful. I had to get rid of the JSON response and leave only cURL status array. In order to do that I added curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); option which cleared all this garbage and left only cURL’s status array.

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://somedomain.com/api/123');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_exec($curl);
$status = curl_getinfo($curl);
curl_close($curl);

Last thing I had to do was retrieve API’s JSON response and assign it to another variable. I have to say the solution wasn’t my first logical choice. After some time it turned out that curl_exec() returns exactly what I need.
In the end, my bit of code became something like this:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://somedomain.com/api/123');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$response = curl_exec($curl);
$status = curl_getinfo($curl);
curl_close($curl);

Now, variable $response contained only JSON and $status contained only cURL’s response status. I was happy again.
 

  • Ralph

    EXACTLY what I needed. Thanks for the example!

  • rob

    lovely man
    lovely blog.

    • shakti

      thanks dude

  • Gary

    Thanks – that was the step I was missing in my own setup!

  • Shamsul

    Thanks. This solved my problem.

  • SP

    Hi,

    Useful tutorial, thanks for sharing.

    I am also trying to obtain some JSON data from an URL; I have to send some variables with POST first.

    My code looks somewhat similar:

    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    //execute post
    $result = curl_exec($ch);

    but I’m not getting anything in that $result var.

    Help…please?:)

    • http://allurcode.com 6bytes

      Does doing this
      $status = curl_getinfo($ch);
      give you any result?

  • SP

    Yes,

    array(26) { ["url"]=> string(46) “http://www.someurl.here/api/idx” ["content_type"]=> string(24) “text/html; charset=UTF-8″ ["http_code"]=> int(200) ["header_size"]=> int(1892) ["request_size"]=> int(204) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(10.671) ["namelookup_time"]=> float(0) ["connect_time"]=> float(0.016) ["pretransfer_time"]=> float(0.016) ["size_upload"]=> float(57) ["size_download"]=> float(23943455) ["speed_download"]=> float(2243787) ["speed_upload"]=> float(5) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(57) ["starttransfer_time"]=> float(2.496) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } ["primary_ip"]=> string(13) “a.b.c.d” ["primary_port"]=> int(xx) ["local_ip"]=> string(12) “x.y.z.t” ["local_port"]=> int(xxxx) ["redirect_url"]=> string(0) “” }