Neeko
Neeko

Reputation: 341

How do I display PHP-cURL errors?

What could be causes of the page not loading? I'm currently sending a request to my bot which sends me a response back but somehow, if the bot is online and working, the page doesn't load. Is there a way to get the error? I have a cPanel and display_errors is 1. Any other ideas?

This is one of my requests:

function getData($Id) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL,"http://IPAdress:8000/Data/".$Id);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    $server_output = curl_exec($ch);
    curl_close ($ch);

   return $server_output;
}

Upvotes: 1

Views: 6577

Answers (1)

hanshenrik
hanshenrik

Reputation: 21463

curl_errno() and curl_error() usually suffice,

$server_output = curl_exec($ch);
$errstr=null;
if(curl_errno($ch)){
    $errstr = curl_errno($ch).": ".curl_error($ch);
}
curl_close ($ch);
if(isset($errstr)){
    echo $errstr;
}

sometimes you can get even more information by adding CURLOPT_VERBOSE

$stderrh=tmpfile();
curl_setopt_array($ch,array(CURLOPT_VERBOSE=>1,CURLOPT_STDERR=>$stderrh));
$server_output = curl_exec($ch);
/* https://bugs.php.net/bug.php?id=76268 */
rewind($stderrh); 
$stderr=stream_get_contents($stderrh);
fclose($stderrh);
$errstr=null;
if(curl_errno($ch)){
    $errstr = curl_errno($ch).": ".curl_error($ch)." verbose: ".$stderr;
}
curl_close ($ch);
if(isset($errstr)){
    echo $errstr;
}

but that's usually kindof overkill

Upvotes: 2

Related Questions