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).

 

:-\

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.