博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP 中和 HTTP 相关的函数及使用
阅读量:5031 次
发布时间:2019-06-12

本文共 7103 字,大约阅读时间需要 23 分钟。

 方法:取得服务器响应一个 HTTP 请求所发送的所有标头

例如:

 

使用 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 OKServer: bfe/1.0.8.5Date: Sat, 24 Oct 2015 05:20:20 GMTContent-Type: text/html; charset=utf-8Transfer-Encoding: chunkedConnection: keep-aliveVary: Accept-EncodingCache-Control: privateCxy_all: baidu+acb608f22c032a75f48a12dcc3a4e51cExpires: Sat, 24 Oct 2015 05:19:22 GMTX-Powered-By: HPHPX-UA-Compatible: IE=Edge,chrome=1BDPAGETYPE: 1BDQID: 0x9b2b20af00151024BDUSERID: 0Set-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 方法 和 变量

使用 file_get_contents 打开一个 URL 时,会创建一个 $http_response_header 变量保存 HTTP 响应头信息(和 get_geaders 方法类似,get_headers 方法可以用过第二个参数是指定返回的保存 HTTP 响应头信息的数组是索引数组还是关联数组。而 $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 方法 和 方法:从封装协议文件指针中取得报头/元数据

 

返回

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] => )

 

 

 方法 和 file_get_contents 方法发送 POST 请求,模拟简单的灌输机器人

在构建 HTTP 请求头信息之前,可以先真实提交,查看真实提交的 HTTP 请求头信息:

 

post 的信息有:

 

 如果返回以下内容则说明发送成功:

 

代码:

1 
'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=&notuser=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; LiveWSPAT63435038sessionid29 =1445669412671927548778; NPAT63435038lastinvite=1445669563512; CNZZDATA1000207770=502543333-1445663698-30 %7C1445663698; CNZZDATA1256212238=1641575784-1445664133-%7C1445664133; browse=aid%3A1083%2C%u9F99%u51E431 %u53E4%u7B5D-%u6960%u6728%u7D20%u9762%2Chttp%3A//www.bj-tygy.com/plus/view.php%3Faid%3D1083%23%23%2332 %2C14053232919107.jpg%7C14053233629812.jpg%7C14053233679827.jpg%7C14053233949645.jpg%2C10%u670824%u65E533 %2014%3A50%3A40; Hm_lvt_db0f997da69f1fbbefb1983923807f95=1445669441; Hm_lpvt_db0f997da69f1fbbefb1983923807f9534 =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' => $data38 )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,'
';51 //var_dump($http_response_header);52 }53 $end = getCurrentTime();54 $spend = $end - $begin;55 echo $spend."s\n";

这里使用了 方法:使用给出的关联(或下标)数组生成一个经过 URL-encode 的请求字符串。

输出:

说明发送 POST 请求成功。

转载于:https://www.cnblogs.com/dee0912/p/4906652.html

你可能感兴趣的文章
ios __block typeof 编译错误解决
查看>>
android 插件形式运行未安装apk
查看>>
ios开发之 manage the concurrency with NSOperation
查看>>
Android权限 uses-permission
查看>>
NSEnumerator用法小结
查看>>
vim如何配置go语言环境
查看>>
机器学习好网站
查看>>
python 中的 sys , os 模块用法总结
查看>>
解题:国家集训队 Middle
查看>>
响应者链
查看>>
指针从函数内部带回返回值
查看>>
在使用webView播放flash或视频文件时无法关闭声音的问题
查看>>
redhat 7 源码安装 mysql5.5.49
查看>>
CCP浅谈
查看>>
NAT虚拟网络配置
查看>>
c#部分---需要实例化的内容;
查看>>
销售类
查看>>
技术项目,问题
查看>>
线程池总结
查看>>
Learning to rank (software, datasets)
查看>>