Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php curl_exec returns empty

Tags:

php

curl

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_PROXY, $proxy); // $proxy is ip of proxy server
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
 $httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); // this results 0 every time
 echo '<br> curl'.$ch; //this line outputs resource id#5
 $exec = stripslashes(curl_exec($ch)); 
 echo '<br> exec'.curl_exec($ch); //this results blank

i am confused why $exec does not return anything ,i am new to curl please help, thanks in advance

like image 434
user3912994 Avatar asked Nov 25 '14 07:11

user3912994


3 Answers

curl_exec will return false when the request failed. Adjust your function to this :

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy); // $proxy is ip of proxy server
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); // this results 0 every time
$response = curl_exec($ch);

if ($response === false) 
    $response = curl_error($ch);

echo stripslashes($response);

curl_close($ch);

This way u'll see the curl error

like image 105
DarkBee Avatar answered Sep 18 '22 19:09

DarkBee


Try:

curl_setopt($ch, CURLOPT_TIMEOUT, 50);

Maybe the response is longer than 10.

I had the same issue, I solved like this.

like image 38
aymen zitoun Avatar answered Sep 19 '22 19:09

aymen zitoun


You're trying to access a HTTP response code before you actually make the HTTP call. Reverse the execution as follows:

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); // this results 0 every time
like image 20
J Marshall Presnell Avatar answered Sep 19 '22 19:09

J Marshall Presnell