Jump to content

if curl takes 60 seconds then echo error


newbtophp

Recommended Posts

I need some help please, I have some code like:

 

        $ch = curl_init(); // initialize curl handle
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_VERBOSE, 0);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
        curl_setopt($ch, CURLOPT_AUTOREFERER, false);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,7);
        curl_setopt($ch, CURLOPT_REFERER, 'http://site.com/upload/');
        curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
        curl_setopt($ch, CURLOPT_FAILONERROR, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
        curl_setopt($ch, CURLOPT_TIMEOUT, 60); // times out after 60s
        curl_setopt($ch, CURLOPT_POST, 1); // set POST method
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); // add POST fields
        $buffer = curl_exec($ch); // run the whole process
        curl_close($ch);

 

I want to echo some text if the curl takes over 60 seconds to respond (from the other site).

 

:-\

i don't know that you can get a read off of the CURL_OPT, but you can always check if the HTTP Response code(s) that are sent back by the external server(s).

 

an HTTP response code for Timeout is 408 .. so, an example (and this is strictly an example), would be something like this:

 

<?php
if ($http_response == '408')
{ echo 'cURL timed out.'; }
?>

 

of course, $http_reponse being whatever the post_back response is of the server that you are requesting information.

 

http status codes

 

again, just an example, but it's how i'd do it (assuming CURL_OPT_TIMEOUT doesn't have a built-in function to handle this, which i'm not sure they do .. check it out first).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.