Code more, but relatively simple, a glance through the, 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 this problem, the process encountered get_headers this function, is sad, recorded, lest Up-and trample on the pit.
Update the record, the function is slightly changed, the return value is basically the same as the result of the previous serialization, not consider supporting the child also supports arrays (consider the details of the performance, but also want to remove the useless HTTP headers ...)
The requirement is simple: get the head information of the picture.
When you debug a program, you find that the call to this function is slow, even if you bind IP, you can jump to more than 20 seconds at a time.
It's time to add a time-out, but looking at the official documentation, the export function interface is as follows:
Copy Code code as follows:
Array get_headers (string$url[,int$format=0])
You're not mistaken, this thing has no timeout interface ...
GitHub look at the source, expect to use his underlying implementation to achieve a new set:
Address HTTPS://GITHUB.COM/PHP/PHP-SRC/BLOB/88CA46D92BC1C426E7C7F7313F0FD2B7DCC33CF6/EXT/STANDARD/URL.C
Copy 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, neither Zend_parse_parameters nor Zend_num_args has a PHP version of the export function.
So the wheels started:
Copy Code code 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 result:
The original is quite a long wait, do not know what to check (did not continue chasing code, interested in the children's shoes can go to play with the next):
Copy Code code 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]=>[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
)
Wheel version return (instantaneous return, the two content slightly different, you look closely you can find some interesting places):
Copy 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]=>[Content-length]=>0
)