How to catch cURL errors in PHP

I am using PHP curl functions to post data to the web server from my local machine. My code is as follows:

$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($c);
if (curl_exec($c) === false) {
    echo "ok";
} else {
    echo "error";
}
curl_close($c);

Unfortunately I am not able to catch any errors like 404, 500 or network failure. So how will I get to know that data was not posted to or retrieved from the remote?

5 Answers
5

Leave a Comment