專業快速廢棄物處理,全年無休風雨無阻.
免費估價,可簽立契約,安全有保障
消毒完美打造店面招牌,免費製稿設計
專業施工,壓克力燈箱,可客製規格訂製,歡迎洽詢

首頁  •  tw 論壇 • 程式設計討論     • 

[php] php原生或curl獲取http headers

房東:王曉貞
發表時間:2016-01-20
[檢舉]


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


Parameters


url

The target URL.


format

If the optional format parameter is set to non-zero, get_headers() parses the response and sets the array\'s keys .


設置為非0返會解析響應關聯數組。

Return Values


Returns an indexed or associative array with the headers, or FALSE on failure.


<?php

$url = \'http://www.example.com\' ;



print_r ( get_headers ( $url ));



print_r ( get_headers ( $url , 1 ));

?>



 Array    (        [ 0] => HTTP/1.1 200 OK        [ 1] => Date : Sat, 29 May 2004 12:28:13 GMT        [ 2] => Server: Apache/1.3.27 (Unix) (Red-Hat/ Linux)        [ 3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT        [ 4] => ETag: "3f80f-1b6-3e1cb03b"        [ 5] => Accept-Ranges: bytes        [ 6] => Content-Length: 438        [ 7] => Connection: close        [ 8] => Content-Type: text/ html    )     Array    (        [ 0] => HTTP/1.1 200 OK        [ Date ] => Sat, 29 May 2004 12:28:14 GMT        [Server] => Apache/1.3.27 (Unix) (Red-Hat/ Linux)        [Last -Modified] => Wed, 08 Jan 2003 23:11:55 GMT        [ETag] => "3f80f-1b6-3e1cb03b"        [Accept -Ranges] => bytes        [Content -Length] => 438        [Connection] => close        [Content -Type] => text/ html    )


http://php.net/manual/en/function.get-headers.php


array getallheaders ( void )

Fetches all HTTP headers from the current request.


This function is an alias for apache_request_headers() . Please read the apache_request_headers() documentation for more information on how this function works.


An associative array of all the HTTP headers in the current request, or FALSE on failure.


我們可以自己寫這個函數:


在PHP裡,想要得到所有的HTTP請求頭,可以使用 getallheaders 方法,不過此方法並不是在任何環境下都存在,比如說,你使用fastcgi方式運行PHP的話,就沒有這個方法,所以說我們還需要考慮別的方法,幸運的是$_SERVER裡有我們想要的東西,它裡面鍵名以HTTP_開頭的就是HTTP請求頭:



$headers = array();

foreach (
$_SERVER as $key => $value ) {

if (
\'HTTP_\' == substr ( $key , 0 , 5 )) {

$headers [ str_replace ( \'_\' , \'-\' , substr ( $key , 5 ))] = $value ;

}

}




代碼很簡單,需要說明的是RFC裡明確指出了信息頭的名字是不區分大小寫的。



不過並不是所有的HTTP請求頭都是以HTTP_開頭的的鍵的形式存在與$_SERVER裡,比如說Authorization,Content-Length,Content-Type就不是這樣,所以說為了取得所有的HTTP請求頭,還需要加上下面這段代碼:



 if ( isset ( $_SERVER [\'PHP_AUTH_DIGEST\' ])) {         $header [\'AUTHORIZATION\'] = $_SERVER [\'PHP_AUTH_DIGEST\' ]);     } elseif ( isset ( $_SERVER [\'PHP_AUTH_USER\']) && isset ( $_SERVER [\'PHP_AUTH_PW\' ])) {         $header [\'AUTHORIZATION\'] = base64_encode ( $_SERVER [\'PHP_AUTH_USER\'] . \':\' . $_SERVER [\'PHP_AUTH_PW\' ]));     }     if ( isset ( $_SERVER [\'CONTENT_LENGTH\' ])) {         $header [\'CONTENT-LENGTH\'] = $_SERVER [\'CONTENT_LENGTH\' ];     }     if ( isset ( $_SERVER [\'CONTENT_TYPE\' ])) {         $header [\' CONTENT-TYPE \'] = $_SERVER[\'CONTENT_TYPE\' ]; //注意-和下劃線不同 }


http://php.net/manual/zh/function.getallheaders.php


我用$_SERVER[\'CONTENT_TYPE\']出現


Undefined index: CONTENT_TYPE。


curl設置header:http://blog.51yip.com/php/1297.html


curl獲取content type



 <? php      # the request    $ch = curl_init(\'http://www.google.com\' );      curl_setopt( $ch , CURLOPT_RETURNTRANSFER, true );      curl_exec( $ch );       # get the content type    echo curl_getinfo($ch, CURLINFO_CONTENT_TYPE);       # output    text/html; charset=ISO-8859-1  ?>


參考:http://stackoverflow.com/questions/2610713/get-mime-type-of-external-file-using-curl-and-php


我們curl_getinfo如果後面只有一個參數,返回一個array


$httpinfo=curl_getinfo($ch);

print_r( $httpinfo);


返回如:



 Array    (        [url] => http: // www.php10086.com/2012/04/605.html     [content_type] => text/html; charset=UTF-8        [http_code] => 200        [header_size] => 344        [request_size] => 71        [filetime] => -1        [ssl_verify_result] => 0        [redirect_count] => 0        [total_time] => 1.545        [namelookup_time] => 0        [connect_time] => 0.172        [pretransfer_time] => 0.172        [size_upload] => 0        [size_download] => 54618        [speed_download] => 35351        [speed_upload] => 0        [download_content_length] => -1        [upload_content_length] => 0        [starttransfer_time] => 0.593        [redirect_time] => 0        [certinfo] => Array            (            )         [primary_ip] => 173.254.29.116        [primary_port] => 80        [local_ip] => 192.168.1.5        [local_port] => 49970        [redirect_url] =>     )



  •  





  • 贊助網站       

    廣利不動產-新板特區指名度最高、值得您信賴的好房仲
    您的托付,廣利用心為您服務
    廣利不動產-板橋在地生根最實在--新板特區指名度最高、值得您信賴的好房仲
    完整房訊,房屋、店面熱門精選物件,廣利不動產 優質仲介,房屋租賃、買賣資訊透明,交易真安心!



  •  共 0 人回應

    姓名:
    佈告內容: