Home  >  Article  >  Backend Development  >  How to get the header information of response when requesting url in php

How to get the header information of response when requesting url in php

coldplay.xixi
coldplay.xixiOriginal
2020-08-03 13:31:033433browse

How to get the header information of the response when requesting the url: 1. When fetching the url, create a variable named [$http_response_header] to save the header of the http response; 2. Use fopen to open the data flow information, Use [stream_get_meta_data] to obtain.

How to get the header information of response when requesting url in php

How php gets the header information responded when requesting the url:

1. Use file_get_contents# When ## or fopen, file, readfile and other functions read the url, a variable named $http_response_header will be created. Save the header of the http response.

Sample code one:

<?php 
       $url = &#39;http://www.baidu.com&#39;; 
       $html = file_get_contents($url); 
       print_r($http_response_header); 
    //输出结果 
       Array( 
            [0] => HTTP/1.1 200 OK 
            [1] => Date: Tue, 06 Nov 2012 08:51:01 GMT 
            [2] => Server: BWS/1.0 
            [3] => Content-Length: 9803 
            [4] => Content-Type: text/html;charset=gbk 
            [5] => Cache-Control: private 
            [6] => Expires: Tue, 06 Nov 2012 08:51:01 GMT 
            [7] => Set-Cookie: BAIDUID=6635735B51B28640F425F802C49340F2:FG=1;  
            expires=Tue, 06-Nov-42 08:51:01 GMT; path=/; domain=.baidu.com 
            [8] => P3P: CP=" OTI DSP COR IVA OUR IND COM " 
            [9] => Connection: Close 
      )       
  ?>

2. Data stream information opened using functions such as fopen can be obtained using

stream_get_meta_data.

Sample code two:

   <?php 
     $fp = fopen($url, &#39;r&#39;); 
     print_r(stream_get_meta_data($fp)); 
     fclose($fp); 
//输出结果 
Array 
( 
    [wrapper_data] => Array 
        ( 
            [0] => HTTP/1.1 200 OK 
            [1] => Date: Tue, 06 Nov 2012 08:54:22 GMT 
            [2] => Server: BWS/1.0 
            [3] => Content-Length: 9803 
            [4] => Content-Type: text/html;charset=gbk 
            [5] => Cache-Control: private 
            [6] => Expires: Tue, 06 Nov 2012 08:54:22 GMT 
            [7] => Set-Cookie: BAIDUID=347578BCBD709F27925BDD8B05364A73:FG=1; 
             expires=Tue, 06-Nov-42 08:54:22 GMT; path=/; domain=.baidu.com 
            [8] => P3P: CP=" OTI DSP COR IVA OUR IND COM " 
            [9] => Connection: Close 
        ) 
   
    [wrapper_type] => http 
    [stream_type] => tcp_socket 
    [mode] => r 
    [unread_bytes] => 0 
    [seekable] =>  
    [uri] => http://www.baidu.com 
    [timed_out] =>  
    [blocked] => 1 
    [eof] =>  
) 
?>

3,

get_headers()You can also get the response message of the request url.

Sample code three:

<?php 
print_r(get_headers($url)); 
Array 
( 
    [0] => HTTP/1.1 200 OK 
    [1] => Date: Tue, 06 Nov 2012 08:58:41 GMT 
    [2] => Server: BWS/1.0 
    [3] => Content-Length: 9803 
    [4] => Content-Type: text/html;charset=gbk 
    [5] => Cache-Control: private 
    [6] => Expires: Tue, 06 Nov 2012 08:58:41 GMT 
    [7] => Set-Cookie: BAIDUID=87B6F26EEC74F2B8F7FABA934DC6BB24:FG=1;  
    expires=Tue, 06-Nov-42 08:58:41 GMT; path=/; domain=.baidu.com 
    [8] => P3P: CP=" OTI DSP COR IVA OUR IND COM " 
    [9] => Connection: Close 
) 
?>

Related learning recommendations:

php graphic tutorial

The above is the detailed content of How to get the header information of response when requesting url in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn