douke1891 2017-03-10 20:03
浏览 59
已采纳

PHP:设置超时的调用URL?

I want to call a certain URL in my PHP script and if I do not get a response after 10s, the script should just continue. Does anyone knows how I can do that?

I only found two ways. One is fopen(), which determinates my script if it doesn't get a return and curl which is just calling the URL without waiting/getting response?

But how can I say, try to get the content of the URL and if you do not get anything after 10s then continue and ignore the URL?

  • 写回答

1条回答 默认 最新

  • dsy48837 2017-03-10 20:28
    关注

    You can use curl.

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "url");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $response = curl_exec($ch);
    curl_close($ch);
    

    If timeout is occurred - $response variable has boolean 'false' value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?