How to obtain curl exec response How to obtain curl exec response curl curl

How to obtain curl exec response


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):

<?phpfunction 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 stringdie;

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 neededvar_dump( json_decode( $stringJSON ) ); // if JSON data was valid, you should get a valid PHP data structure