How to obtain curl exec response

Viewed 8479

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

1 Answers
Related