PHP get_headers to determine if the 200 encountered a non-existent script execution timeout

Source: Internet
Author: User
PHP get_headers to determine if the return is 200 encountered a non-existent script execution timeout
A nonexistent IP URL will wait for a response until the function times out, causing the page to be too slow to display.

Failed to open Stream:http request failed!
Fatal error:maximum execution time of seconds exceeded

Set the Ini_set (' Max_execution_time ', 10);

Do not use this function to write the socket itself, with this function of the curl_init, trouble, what do you have a good way not


------Solution--------------------
discuss

I'm not going to let him do that all the time. Execute for up to 5 seconds, let him stop, but don't hold the wrong

------Solution--------------------
discuss

Dizzy, csdn original more dull, old, reply to how long time has not come out

I don't want to let PHP go on, give him a time like 5 seconds, no results, no, directly judge the following on the line
Set_time_limit (5) If you use this, the program stops directly.

------Solution--------------------
Resource Fsockopen (string $hostname [, int $port =-1 [, int & $errno [, String & $errstr [, float $timeout = ini_ Get ("Default_socket_timeout")]])
The last parameter $timeout set to 5, the maximum connection time is 5 seconds, the expiration function returns, and the program does not exit
------Solution--------------------
Thinking is upstairs, get_headers can only say PHP this function is not perfect, we only use the socket to write their own
Because I can not give their own points, I use another account landing, the following is the complete code, but also to others to contribute to the
PHP Code
function Doget ($url) {$url 2 = Parse_url ($url);    if (!isset ($url 2[' path ')) $url 2[' path ']= '; $url 2["path"] = ($url 2["path"] = = ""? "    /": $url 2[" path "]);    $url 2["port"] = (isset ($url 2["port")? $url 2["Port"]:80);    $host _ip = @gethostbyname ($url 2["host"]);  $fsock _timeout = 2; 2 Second if (! (        $fsock = Fsockopen ($host _ip, $url 2["Port"], $errno, $errstr, $fsock _timeout)) {//echo ' Fsockopen fail ';    return false; } $request = $url 2["path"]. (Isset ($url 2["Query"])? "?".            $url 2["Query"]: ""); $in = "GET". $request.    "Http/1.0\r\n";    $in. = "Accept: */*\r\n";    $in. = "user-agent:payb-agent\r\n"; $in. = "Host:". $url 2["Host"].    "\ r \ n";            $in. = "connection:close\r\n\r\n";        Stream_set_timeout ($fsock, $fsock _timeout);        if ([email protected] ($fsock, $in, strlen ($in))) {//echo ' fwrite fail ';        Fclose ($fsock);    return false;    } $status = Stream_get_meta_data ($fsock); Send data timeout if ($status [' timed_out ']) {//echo "Write Time Out";        Fclose ($fsock);    return false;    } $out = null;        while ($buff = @fgets ($fsock, 2048)) {$out. = $buff;    Read Only header information if (False!==strpos ($out, "\r\n\r\n")) break;    } $status = Stream_get_meta_data ($fsock);        Read Data timeout if ($status [' timed_out ']) {//echo "Read Time Out";        Fclose ($fsock);    return false;        } fclose ($fsock);    $pos = Strpos ($out, "\r\n\r\n");    $head = substr ($out, 0, $pos); HTTP head return $head;} $rs = doget ("http://www.ba05afasdfsiddf08usfa.com"), if (! $rs) echo ' fail '; else echo $rs;
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.