PHP implementation with timeout function Get_headers function, phpget_headers_php tutorial

Source: Internet
Author: User
Tags ranges varnish

PHP implementation with timeout function Get_headers function, phpget_headers


Code is more, but relatively simple, a glance to see through, so, writing as little as possible.
Because of the well-known network reasons, Gavatar also began to slow down, wrote a small thing to solve the problem, the process encountered get_headers this function, very sad, recorded, so as not to Yimeimei trample pits.
Update the record, the function is slightly changed, the return value is basically the same as the result of the previous serialization, the support for the child is not considered to support the array, etc. (consider the details of performance, but also want to cut off the useless HTTP head ...)
The requirements are simple: get the head information for the picture.
The debugger found that the call to this function is very slow, even if the IP is bound, sometimes can jump to more than 20 seconds.
Think about it or you should add a timeout, but look at the official document, the exported function interface is as follows:
Copy the Code code as follows:
Array get_headers (string$url[,int$format=0])

You are not mistaken, this thing has no timeout interface ...
On GitHub to look at the source code, expect to be able to use his bottom realization to re-implement a set:
Address https://github.com/php/php-src/blob/88ca46d92bc1c426e7c7f7313f0fd2b7dcc33cf6/ext/standard/url.c#L710

Copy the Code code 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 other ... **/
}
/* }}} */

Unfortunately, Zend_parse_parameters and Zend_num_args do not have a PHP version of the export function.
So the wheel began to build:
Copy CodeThe 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;
}

Compare the final results:
The original is a pretty long wait, do not know what to verify (not continue to chase the code, interested children shoes can go to play with):

Copy CodeThe code is as follows:
Array
(
[0]=>http/1.0302found
[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/[omitted ...]
[Server]=>array
(
[0]=>ecs (oxr/838b)
[1]=>nginx
)

[source-age]=>85
[Via]=>1.1varnish
[X-cache]=>302-hit
[x-varnish]=>14702550881470006304
[Content-length]=>0
[Connection]=>array
(
[0]=>close
[1]=>close
)

[1]=>http/1.1504gateway Timeout
)

The wheel version returns (instantly returns, the two contents are slightly different, you can look closely to find some interesting places):

Copy the Code code as follows:
Array
(
[0]=>http/1.1302found
[Accept-ranges]=>bytes
[Via]=>1.1varnish
[cache-control]=>max-age=300
[Server]=>ecs (oxr/838b)
[Content-type]=>text/html;charset=utf-8
[x-varnish]=>14702550881470006304
[Date]=>fri,12dec201420:31:02gmt
[location]=>http://i2.wp.com/[omitted ...]
[Expires]=>fri,12dec201420:36:02gmt
[source-age]=>85
[Last-modified]=>wed,11jan198408:00:00gmt
[X-cache]=>302-hit
[link]=> ; rel= "Canonical"
[Content-length]=>0
)

http://www.bkjia.com/PHPjc/955976.html www.bkjia.com true http://www.bkjia.com/PHPjc/955976.html techarticle PHP Implementation of the band timeout function get_headers function, phpget_headers code more, but relatively simple, a glance to see through, so, writing as little as possible. Because the well-known network original ...

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