2

I have a scenario. Here I'm using Curl for accessing an API. If API is not accessible, I have to send an alert email. So how can I check, if there is a request timeout?

Here is my code.

    $alerthttp1 = array();
    $string = 'somestring';
    $key = md5($string);
    $service_url = 'www.someurl.com/'.$key;

    $curl = curl_init($service_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 300);
    $curl_response = curl_exec($curl);
    $curl_errno = curl_errno($curl);
    $curl_error = curl_error($curl);

    error_log(json_encode($curl_errno));
    error_log(json_encode($curl_error));

    if(curl_error($curl) || $curl_response === false || $curl_errno > 0)
    {
        $info = curl_getinfo($curl);
        hlog_errorlog('error occured during curl exec - ' . var_export($info));
        hlog_errorlog('error -----------> '. $curl_error); 
        curl_close($curl);

        $alerthttp1[] = array('status' => 'FAILED');//for email alert
    }

Please suggest me, the best way of coding.

Thank you

1

2 Answers 2

2

Try to use CURLOPT_CONNECTTIMEOUT instead CURLOPT_TIMEOUT CURLOPT_CONNECTTIMEOUT is the maximum amount of time in seconds that is allowed to make the connection to the server.

Sign up to request clarification or add additional context in comments.

Comments

0

No you can't know it with CURL. Ask on API support. Or choose timeout time yourself. Usually it's 30 second.

1 Comment

CURLOPT_CONNECTTIMEOUT tells cURL the maximum amount of time to wait for the connection to complete so it will work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.