① get_headers 方法:取得服务器响应一个 HTTP 请求所发送的所有标头
例如:
<?php $httpinfo = get_headers('http://www.baidu.com', 1); print_r($httpinfo);
使用 FF 的查看页面源码查看返回的结果(如果省略第二个参数,返回的则是索引数组):
Array ( [0] => HTTP/1.1 200 OK [Date] => Sat, 24 Oct 2015 05:20:47 GMT [Content-Type] => text/html [Content-Length] => 14613 [Last-Modified] => Tue, 02 Sep 2014 08:55:13 GMT [Connection] => Close [Vary] => Accept-Encoding [Set-Cookie] => Array ( [0] => BAIDUID=57422D9DF880F51C9236D0AAC5AB11BA:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [1] => BIDUPSID=57422D9DF880F51C9236D0AAC5AB11BA; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [2] => PSTM=1445664047; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [3] => BDSVRTM=0; path=/ ) [P3P] => CP=" OTI DSP COR IVA OUR IND COM " [Server] => BWS/1.1 [X-UA-Compatible] => IE=Edge,chrome=1 [Pragma] => no-cache [Cache-control] => no-cache [BDPAGETYPE] => 1 [BDQID] => 0xe5d2f28c001a4c17 [BDUSERID] => 0 [Accept-Ranges] => bytes )
而使用 FF 直接访问百度(未登录),返回的 HTTP 响应头信息:
HTTP/1.1 200 OK Server: bfe/1.0.8.5 Date: Sat, 24 Oct 2015 05:20:20 GMT Content-Type: text/html; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Vary: Accept-Encoding Cache-Control: private Cxy_all: baidu+acb608f22c032a75f48a12dcc3a4e51c Expires: Sat, 24 Oct 2015 05:19:22 GMT X-Powered-By: HPHP X-UA-Compatible: IE=Edge,chrome=1 BDPAGETYPE: 1 BDQID: 0x9b2b20af00151024 BDUSERID: 0 Set-Cookie: BDSVRTM=0; path=/ BD_HOME=0; path=/ H_PS_PSSID=17522_1456_17619_17640_17001_17471_17073_15917_12248_17051; path=/; domain=.baidu.com __bsi=11400537099011293757_00_0_I_R_4_0303_C02F_N_I_I_0; expires=Sat, 24-Oct-15 05:20:25 GMT; domain=www.baidu.com; path=/ Content-Encoding: gzip
一般使用 get_headers 方法请求一个 URL,根据其返回的数据判断状态码是否为 200 来判断所请求的资源是否存在。如果失败则返回 False 并发出一条 E_WARNING 级别的错误信息。
② file_get_contents 方法 和 $http_response_header 变量
使用 file_get_contents 打开一个 URL 时,会创建一个 $http_response_header 变量保存 HTTP 响应头信息(和 get_geaders 方法类似,get_headers 方法可以用过第二个参数是指定返回的保存 HTTP 响应头信息的数组是索引数组还是关联数组。而 $http_response_header 保存的是索引数组):
<?php $html = file_get_contents('http://www.baidu.com/'); print_r($http_response_header);
返回:
Array ( [0] => HTTP/1.1 200 OK [1] => Date: Sat, 24 Oct 2015 05:30:13 GMT [2] => Content-Type: text/html [3] => Content-Length: 14613 [4] => Last-Modified: Tue, 02 Sep 2014 08:55:13 GMT [5] => Connection: Close [6] => Vary: Accept-Encoding [7] => Set-Cookie: BAIDUID=BCA76FCEBB02BA1E33EE2936557F4B93:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [8] => Set-Cookie: BIDUPSID=BCA76FCEBB02BA1E33EE2936557F4B93; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [9] => Set-Cookie: PSTM=1445664613; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [10] => Set-Cookie: BDSVRTM=0; path=/ [11] => P3P: CP=" OTI DSP COR IVA OUR IND COM " [12] => Server: BWS/1.1 [13] => X-UA-Compatible: IE=Edge,chrome=1 [14] => Pragma: no-cache [15] => Cache-control: no-cache [16] => BDPAGETYPE: 1 [17] => BDQID: 0xbd11b30400147402 [18] => BDUSERID: 0 [19] => Accept-Ranges: bytes )
③ fopen 方法 和 stream_get_meta_data 方法:从封装协议文件指针中取得报头/元数据
<?php $fp = fopen('http://www.baidu.com/', 'r'); print_r(stream_get_meta_data($fp)); fclose($fp);
返回
Array ( [wrapper_data] => Array ( [0] => HTTP/1.1 200 OK [1] => Date: Sat, 24 Oct 2015 05:45:19 GMT [2] => Content-Type: text/html [3] => Content-Length: 14613 [4] => Last-Modified: Tue, 02 Sep 2014 08:55:13 GMT [5] => Connection: Close [6] => Vary: Accept-Encoding [7] => Set-Cookie: BAIDUID=3D2EF009AB73DEF6A808146A1CB6BEF5:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [8] => Set-Cookie: BIDUPSID=3D2EF009AB73DEF6A808146A1CB6BEF5; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [9] => Set-Cookie: PSTM=1445665519; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com [10] => Set-Cookie: BDSVRTM=0; path=/ [11] => P3P: CP=" OTI DSP COR IVA OUR IND COM " [12] => Server: BWS/1.1 [13] => X-UA-Compatible: IE=Edge,chrome=1 [14] => Pragma: no-cache [15] => Cache-control: no-cache [16] => BDPAGETYPE: 1 [17] => BDQID: 0xb0560df000168912 [18] => BDUSERID: 0 [19] => Accept-Ranges: bytes ) [wrapper_type] => http [stream_type] => tcp_socket [mode] => r [unread_bytes] => 0 [seekable] => [uri] => http://www.baidu.com/ [timed_out] => [blocked] => 1 [eof] => )
④ stream_context_creat 方法 和 file_get_contents 方法发送 POST 请求,模拟简单的灌输机器人
在构建 HTTP 请求头信息之前,可以先真实提交,查看真实提交的 HTTP 请求头信息:
post 的信息有:
如果返回以下内容则说明发送成功:
代码:
1 <?php 2 header("Content-type: text/html; charset=gb2312"); 3 set_time_limit(0); 4 5 $data = array( 6 'dopost' => 'send', 7 'comtype' => 'comments', 8 'aid' => 1083, 9 'fid' => 0, 10 'msg' => '轻轻的我来了', 11 'username' => 'car', 12 'notuser' => '', 13 'face' => 6, 14 'feedbacktype' => 'feedback', 15 'pwd' => '', 16 'validate' => '', 17 ); 18 19 $data = http_build_query($data); 20 //echo $data; 21 //dopost=send&comtype=comments&aid=1083&fid=0&msg=%C7%E1%C7%E1%B5%C4%CE%D2%C0%B4%C1%CB&username=car&face=6&feedbacktype=feedback&pwd=&validate=¬user= 22 23 $opts = array( 24 'http' => array( 25 'method' => 'POST', 26 'header' => "Content-type:application/x-www-form-urlencoded\r\n". 27 "Content-length:".strlen($data)."\r\n". 28 "Cookie:PHPSESSID=ee6jp9rhlom89p0apja3f7p983; LiveWSPAT63435038=1445669412671927548778; LiveWSPAT63435038sessionid 29 =1445669412671927548778; NPAT63435038lastinvite=1445669563512; CNZZDATA1000207770=502543333-1445663698- 30 %7C1445663698; CNZZDATA1256212238=1641575784-1445664133-%7C1445664133; browse=aid%3A1083%2C%u9F99%u51E4 31 %u53E4%u7B5D-%u6960%u6728%u7D20%u9762%2Chttp%3A//www.bj-tygy.com/plus/view.php%3Faid%3D1083%23%23%23 32 %2C14053232919107.jpg%7C14053233629812.jpg%7C14053233679827.jpg%7C14053233949645.jpg%2C10%u670824%u65E5 33 %2014%3A50%3A40; Hm_lvt_db0f997da69f1fbbefb1983923807f95=1445669441; Hm_lpvt_db0f997da69f1fbbefb1983923807f95 34 =1445669441; NPAT63435038LR_check_data=4%7C1445669563666%7C%7C%7C"."\r\n". 35 "Referer:http://www.bj-tygy.com/plus/view.php?aid=1083"."\r\n". 36 "User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:41.0) Gecko/20100101 Firefox/41.0"."\r\n", 37 'content' => $data 38 ) 39 ); 40 $context = stream_context_create($opts); 41 42 function getCurrentTime (){ 43 list ($msec,$sec) = explode(" ", microtime()); 44 return (float)$msec + (float)$sec; 45 } 46 47 $begin = getCurrentTime(); 48 for($i = 0; $i < 10; $i ++){ 49 $html = @file_get_contents('http://www.bj-tygy.com/plus/feedback_ajax.php?dopost=send&aid=1083', false, $context); 50 echo ($i+1),$html,'<br />'; 51 //var_dump($http_response_header); 52 } 53 $end = getCurrentTime(); 54 $spend = $end - $begin; 55 echo $spend."s\n";
这里使用了 http_build_query 方法:使用给出的关联(或下标)数组生成一个经过 URL-encode 的请求字符串。
输出:
说明发送 POST 请求成功。