在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
有一个函数: array get_headers ( string Parameters url format 设置为非0返会解析响应关联数组。 Returns an indexed or associative array with the headers, or FALSE on failure.
<?php 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 我们可以自己写这个函数: 在PHP里,想要得到所有的HTTP请求头,可以使用getallheaders方法,不过此方法并不是在任何环境下都存在,比如说,你使用fastcgi方式运行PHP的话,就没有这个方法,所以说我们还需要考虑别的方法,幸运的是$_SERVER里有我们想要的东西,它里面键名以HTTP_开头的就是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); 返回如: 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] => )
|
2022-08-17
2022-11-06
2022-08-17
2022-07-18
2022-07-18
请发表评论