Phpget_headers: determines whether the returned result is 200 and the execution of a nonexistent script times out.

Source: Internet
Author: User
Phpget_headers checks whether the returned IP address is 200 when a nonexistent script execution times out and does not exist. it waits until the function times out, causing the page to be delayed. Failedtoopenstream: HTTPrequestfailed! & Nbsp; Fatalerro php get_headers judge whether the returned result is 200 and the execution of a nonexistent script times out.
If the IP address does not exist, it will wait for the response until the function times out, causing the page to be delayed.

Failed to open stream: HTTP request failed!
Fatal error: Maximum execution time of 30 seconds exceeded

Set ini_set ('max _ execution_time ', 10 );

Do you need to use this function to write the socket by yourself? use curl_init of this function. please do not have any good methods.


------ Solution --------------------
Discussion

I can't keep him running it there. if it takes up to five seconds to execute it, let him stop, but don't make a mistake.

------ Solution --------------------
Discussion

Dizzy, csdn is getting slower and old. How long has it not been answered yet?

I don't want php to be executed all the time. I just want to give him a time, for example, 5 seconds. If no result is returned, I don't need to. just judge the following.
If set_time_limit (5) is used in this way, the program will be stopped directly.

------ Solution --------------------
Resource fsockopen (string $ hostname [, int $ port =-1 [, int & $ errno [, string & $ errstr [, float $ timeout = ini_get ("default_socket_timeout")])
The last parameter $ timeout is set to 5, and the maximum connection time is set to 5 seconds. the expired function returns and the program does not exit.
------ Solution --------------------
The idea is upstairs. get_headers can only say that the php function is not perfect. we only need to write it using socket.
Because I cannot score myself, I logged in with another account. The following is the complete code and I have contributed to others.
PHP code
Function DoGet ($ url) {$ url2 = parse_url ($ url); if (! Isset ($ url2 ['path']) $ url2 ['path'] = ''; $ url2 ["path"] = ($ url2 ["path"] = ""? "/": $ Url2 ["path"]); $ url2 ["port"] = (isset ($ url2 ["port"])? $ Url2 ["port"]: 80); $ host_ip = @ gethostbyname ($ url2 ["host"]); $ fsock_timeout = 2; // 2 second if (! ($ Fsock = fsockopen ($ host_ip, $ url2 ["port"], $ errno, $ errstr, $ fsock_timeout) {// echo 'fsockopen fail '; return false;} $ request = $ url2 ["path"]. (isset ($ url2 ["query"])? "? ". $ Url2 ["query"]: ""); $ in = "GET ". $ request. "HTTP/1.0 \ r \ n"; $ in. = "Accept: */* \ r \ n"; $ in. = "User-Agent: Payb-Agent \ r \ n"; $ in. = "Host :". $ url2 ["host"]. "\ r \ n"; $ in. = "Connection: Close \ r \ n"; stream_set_timeout ($ fsock, $ fsock_timeout); if (! @ Fwrite ($ 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 the header information if (false! = Strpos ($ out, "\ 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"); $ 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.