① 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=&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; 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 请求成功。

PHP 中和 HTTP 相关的函数及使用相关推荐

  1. js进阶 13-6 jquery动画效果相关常用函数有哪些

    js进阶 13-6 jquery动画效果相关常用函数有哪些 一.总结 一句话总结:animate(),stop(),finish(),delat()四个. 1.stop()方法的基本用法是什么(sto ...

  2. C语言 system相关的函数

    system相关的函数 clrscr,ClearScreen DispBCD SetScrollBar TextOut,outtextxy TextOut bell block cursor dela ...

  3. C语言 字符串相关的函数

    字符串相关的函数 bcmp bcopy bzero memccpy memchr memcmp memcpy memicmp memmove memset movmem setmem stpcpy s ...

  4. C语言的math相关的函数

    C语言的math相关的函数 fabs abs acos asin atan atan2 ceil cos cosh exp fabs floor fmod hypot ldexp log log10 ...

  5. linux 与信号集操作相关的函数

    与信号集操作相关的函数 #include <signal.h> 清空信号集 全都为0 int sigemptyset(sigset_t *set);填充信号集 全都为1 int sigfi ...

  6. 零基础带你学习MySQL—字符串相关的函数(十三)

    零基础带你学习MySQL-字符串相关的函数(十三) 一.CHARSET (str)返回子串字符集 二.CONCAT连接子串,将多个列拼接成一列 三.INSTR(string,substring)返回s ...

  7. HEVC: 整个编码流程以及相关的函数介绍

    本文转载自https://blog.csdn.net/qq_39348150/article/details/78422117 来自网上的文档,但是最初来源不知道是哪,谢谢这个作者! 整个流程可以从c ...

  8. QT基础之位置相关的函数介绍

    文章目录 代码链接 简介 Demo实例 更多参见 QT基础与实例应用目录 代码链接 GitHub链接 :CoordinateExample 简介 Qt中有很多与位置相关的函数或者位置函数,主要用于获取 ...

  9. 线程及其相关接口函数(Linux)

    目录 一.线程 1.线程相关接口函数 (1)创建线程 pthread_create() (2)结束线程 pthread_exit() (3)等待进程 pthread_join() 2.线程间通信 (1 ...

最新文章

  1. fan怎么写 jin_fanjin怎么写
  2. vector的插入、lower_bound、upper_bound、equal_range实例
  3. 10多所高校发布通知: 今年研究生复试或有变!
  4. Pandas的学习(3.DataFrame的创建方法和三种索引方法(iloc、loc、values)以及切片)
  5. 国内规模最大的商业WiFi运营商百米生活挂牌新三板
  6. 作业6--四则运算APP之Sprint计划
  7. 面向对象基本原则-转载
  8. webmagic框架
  9. mysql怎么导出导入数据库结构_mysql导入SQL、导出数据库、表、结构
  10. Android入门笔记05
  11. 数据库课程设计——实验报告管理系统(超详细)
  12. KDD 2022论文合集(持续更新中)
  13. 固定在计算机主机箱体上的起到连接计算机,固定在计算机主机箱箱体上的、起到连接计算机各种部件的纽带和桥梁作用的是( )。...
  14. 纯粹的Pure Storage,简单却又不简单
  15. 预测混合模式发展前景计算机,基于混合模式的网络流量分类优化-计算机技术专业论文.docx...
  16. 使用Autoit3 自动登录163邮箱
  17. HTTP学习笔记(适合初学)2
  18. IDA静态动态逆向分析基础
  19. 2019年软件评测师真题精选
  20. 英语听力采用计算机化考试,北京高考英语听力机考有什么特点?

热门文章

  1. TCP慢开始与拥塞避免
  2. 使用for of循环遍历获取的nodeList,配置babel编译,webpack打包之后在iphone5下报错...
  3. 公安部起草《“十三五”平安中国建设规划》并公开征求意见
  4. oracle创建表之前判断表是否存在,如果存在则删除已有表
  5. SQL Server中SET赋值和SELECT赋值的区别
  6. 【专题报道】Google I/O开发者大会
  7. Silverlight 游戏开发小技巧:实现街霸4的选人界面
  8. MongoDB数据访问[C#]
  9. Python笔记---错误笔记
  10. fsLayui联动表格使用(二)