Denis Evseev
Denis Evseev

Reputation: 1710

How to obtain curl exec response

I am developing a PHP script that:

  1. Gets 10 rows from DB (works well)
  2. Sends addresses from these rows to Map API
  3. Obtains data
  4. Save results in DB (works well)

I don't know how to obtain the final response of curl_exec. curl_exec in $response = call('GET', $query); - even when hasn't completed returns something in response.

These my loop that is waiting for the response of the call function. But it doesn't work json_decode($stringJSON) is invoked earlier than it is obtained the final response

$requestHandled = false;
$response = "";

$response = call('GET', $query);
while(!$requestHandled) {
    if(strlen($response) > 5){

        $response = mb_convert_encoding($response, "UTF-8");
        $stringJSON = get_magic_quotes_gpc() ? stripslashes($response) : $response;
        echo $stringJSON;

        $jsonObject = "+";
        echo $jsonObject;

        $jsonObject = json_decode($stringJSON);

        echo $jsonObject;

        $requestHandled = true;
    }
}

This is my curl call function

function call($method, $url, $data = false) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_URL, $url);
    if ($data) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $headers = array();
        $headers[] = 'Content-Type: application/json';
        $headers[] = 'Content-Length: ' . strlen($data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    return curl_exec($ch);

Please help. Has spent a half of the day solving it

Upvotes: 5

Views: 11370

Answers (1)

Decent Dabbler
Decent Dabbler

Reputation: 22783

So the result of the var_dump( $response ) is an empty string?

In that case, it simply means the response from the server is empty, because I just tested your function call() and it seems to work just fine. In other words, make sure the URL (with the method GET) you are trying to call actually returns data to begin with.

I wouldn't be surprised if you simply have a typographical error somewhere in the URL.

Also, for debugging purposes, temporarily replace

return curl_exec($ch);

with

$result = curl_exec($ch); 
var_dump(curl_getinfo($ch));
return $result;

and investigate the info in the var_dump() result, to make sure the response code is 200 (or any other that indicates success) and not in the 4xx or 5xx ranges (respectively indicating either a client or server error), for instance.

See curl_getinfo() for more information about what useful information will be returned about your last curl transfer.


To address OPs comment(s):

Please try this complete script (nothing else: no while loop, no stripslashes(), no json_decode(), etc):

<?php

function call($method, $url, $data = false) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_URL, $url);
    if ($data) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $headers = array();
        $headers[] = 'Content-Type: application/json';
        $headers[] = 'Content-Length: ' . strlen($data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch); 
    var_dump(curl_getinfo($ch)); // check this result and make sure the response code is 200
    return $result;
}

$query = 'fill in the correct URL! (with http(s)://)';

$response = call('GET', $query);
var_dump( $response ); // check this result and see if it's an empty string
die;

If the var_dump( $response ); returns an empty string, it means your script works fine but the URL you are calling simply returned an empty response.

If $response is not empty and actually contains JSON data, replace

var_dump( $response );

with

$stringJSON = mb_convert_encoding( $response, "UTF-8" );

echo $stringJSON; // make sure this contains valid JSON data

// stripslashes() should not be needed

var_dump( json_decode( $stringJSON ) ); // if JSON data was valid, you should get a valid PHP data structure

Upvotes: 4

Related Questions