I am using cURL to retrieve a file from a server, I wonder is there a way to specify a return value when curl_exec() fails. My code is like this:
if (!$result = curl_exec($curl)){
echo "something is wrong.";
exit;
}
/**
*the rest of the script
*/
When the exec() call fails, the echo statement is not printed on the page, and apparently the exit() is not called as well, causing my following scripts to execute as normal. I noticed that when curl_exec() fails, it does not give you FALSE, it just gives you a something like “NOT FOUND” page, I guess this is where my problem is. Is there any way that can force it to return a boolean or a string so that I can do the condition test? Many thanks.
curl_execwill only returnfalseif the HTTP request did not complete successfully from a technical standpoint. If the request itself does complete but the result is not what you expected (e.g. the server responded with a 404) it will correctly returntrueor the result (depending onCURLOPT_RETURNTRANSFERbeing set or not).Therefore you have to check for this eventuality separately. For example:
See the list of HTTP status codes for more information.