This is my php code
<?php
/* some php code - top */
/* curl request logic start */
$ch = curl_init("https://api.abc.com/xyz");
$response = curl_exec($ch);`
if (curl_errno($ch))
{
$error_msg = curl_error($ch);
}
curl_close($ch);
if (isset($error_msg))
{
echo 'Curl error: ' . $error_msg;
}
else
{
echo $response;
}
/* curl request logic end */
/* some php code - bottom */
?>
My problem is , if the server for "https://api.abc.com/xyz" is shut down/is not reachable, the entire php script execution freezes.
What I want is that even if the api "https://api.abc.com/xyz" is down, the php code indicated by /* some php code - top */
and /* some php code - bottom */
should get executed and the page should not hang/freeze completely.
Any advice would be much appreciated….!
Thank You
I have tried curl error handling, but it does not seem to work.
3
Answers
You can set a timeout to curl connection and reponse request with:
You need to set the return value as a string.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
?>
Hope this solve your error