The get_headers function with timeout implemented by PHP. phpget_headers_PHP tutorial

Source: Internet
Author: User
Tags varnish
PHP implements the get_headers function with the timeout function, phpget_headers. PHP implements the get_headers function with the timeout function. phpget_headers has a lot of code, but it is relatively simple. it can be seen at a glance, so, and the text should be written as little as possible. As we all know, the get_headers function with the timeout function implemented by the original PHP network, phpget_headers

There are many codes, but they are relatively simple. they can be seen at a glance, so and should be written as little as possible.
Gavatar is also getting slower and slower because of the well-known network reasons. I wrote a small item to solve this problem. in the process, I encountered the get_headers function, which was very sad. I recorded it to avoid further attacks.
Update record. the function is slightly changed. The Returned values are basically the same as the serialized results. Currently, the support for subitems and arrays are not considered (considering the detailed performance, I also want to cut down useless http headers ....)
The requirement is simple: get the head Information of the image.
When debugging the program, it is found that the function call is very slow. even if the ip address is bound, sometimes it can jump to more than 20 seconds.
Think about how to add a timeout. but read the official documentation and the export function interface is as follows:

The code is as follows:


Array get_headers (string $ url [, int $ format = 0])


You are not mistaken. there is no timeout interface for this item...
On github, I looked at the source code and expected to use its underlying implementation to implement a new set:
Address https://github.com/php/php-src/blob/88ca46d92bc1c426e7c7f7313f0fd2b7dcc33cf6/ext/standard/url.c#L710

The code is as follows:


/* {Proto array get_headers (string url [, int format])
Fetches all the headers sent by the server in response to a HTTP request */
PHP_FUNCTION (get_headers)
{
Char * url;
Size_t url_len;
Php_stream_context * context;
Php_stream * stream;
Zval * prev_val, * hdr = NULL, * h;
HashTable * hashT;
Zend_long format = 0;

If (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "s | l", & url, & url_len, & format) = FAILURE ){
Return;
}

/** Omit a bunch of others ...**/
}
/*}}}*/

Unfortunately, zend_parse_parameters and ZEND_NUM_ARGS do not have any export functions for PHP.
As a result, the wheel begins:

The code is as follows:


Functionget_url_headers ($ url, $ timeout = 10)
{
$ Ch = curl_init ();

Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_HEADER, true );
Curl_setopt ($ ch, CURLOPT_NOBODY, true );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
Curl_setopt ($ ch, CURLOPT_TIMEOUT, $ timeout );

$ Data = curl_exec ($ ch );
$ Data = preg_split ('/\ n/', $ data );

$ Data = array_filter (array_map (function ($ data ){
$ Data = trim ($ data );
If ($ data ){
$ Data = preg_split ('/: \ s/', trim ($ data), 2 );
$ Length = count ($ data );
Switch ($ length ){
Case2:
Returnarray ($ data [0] => $ data [1]);
Break;
Case1:
Return $ data;
Break;
Default:
Break;
}
}
}, $ Data ));

Sort ($ data );

Foreach ($ dataas $ key => $ value ){
$ ItemKey = array_keys ($ value) [0];
If (is_int ($ itemKey )){
$ Data [$ key] = $ value [$ itemKey];
} Elseif (is_string ($ itemKey )){
$ Data [$ itemKey] = $ value [$ itemKey];
Unset ($ data [$ key]);
}
}

Return $ data;
}

Comparison results:
The original version is a long wait, and I don't know what to do with verification (I haven't followed up with the code. if you are interested, you can play with it ):

The code is as follows:


Array
(
[0] => HTTP/1.0302 Found
[Accept-Ranges] => bytes
[Cache-Control] => max-age = 300.
[Content-Type] => Array
(
[0] => text/html; charset = utf-8
[1] => text/html; charset = utf-8
)

[Date] => Array
(
[0] => Fri, 12Dec201415: 35: 40GMT
[1] => Fri, 12Dec201415: 35: 43GMT
)

[Expires] => Fri, 12Dec201415: 40: 40GMT
[Last-Modified] => Wed, 11Jan198408: 00: 00GMT
[Link] => ; Rel = "canonical"
[Location] => http://i2.wp.com/[ellipsis ...]
[Server] => Array
(
[0] => ECS (oxr/838B)
[1] => nginx
)

[Source-Age] => 85
[Via] = & gt; 1.1 varnish
[X-Cache] = & gt; 302-HIT
[X-Varnish] = & gt; 14702550881470006304
[Content-Length] => 0
[Connection] => Array
(
[0] => close
[1] => close
)

[1] => HTTP/1.1504 Gateway Timeout
)

Wheel Edition return (instant return, the content of the two is slightly different, you can look at it to find some interesting places ):

The code is as follows:


Array
(
[0] => HTTP/1.1302 Found
[Accept-Ranges] => bytes
[Via] = & gt; 1.1 varnish
[Cache-Control] => max-age = 300.
[Server] => ECS (oxr/838B)
[Content-Type] => text/html; charset = utf-8
[X-Varnish] = & gt; 14702550881470006304
[Date] => Fri, 12Dec201420: 31: 02GMT
[Location] => http://i2.wp.com/[ellipsis ...]
[Expires] => Fri, 12Dec201420: 36: 02GMT
[Source-Age] => 85
[Last-Modified] => Wed, 11Jan198408: 00: 00GMT
[X-Cache] = & gt; 302-HIT
[Link] => ; Rel = "canonical"
[Content-Length] => 0
)

There are many pipeline codes, but they are relatively simple. they can be seen at a glance, so, and the text should be written as little as possible. Because we all know the original network...

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.