天天看点

PHP 发送HTTP post请求发送和获取

发送POST请求

和GET方法一样,POST方法也是HTTP协议中的一个重要组成部分。POST方法一般用来向目的服务器发出请求,并附有请求实体。

POST被设计成用统一的方法实现下列功能:

     o 对现有资源的注释(Annotation of existing resources);

     o 向电子公告栏、新闻组,邮件列表或类似讨论组发送消息;

     o 提交数据块,如将表格(form)的结果提交给数据处理过程;

     o 通过附加操作来扩展数据库。

     o 也可用来上传文件。

在所有的HTTP的POST请求中,必须指定合法的内容长度(Content-Length)。

如果HTTP/1.0服务器在接收到请求消息内容时无法确定其长度,就会返回400(非法请求)代码。

应用程序不能缓存对POST请求的回应,因为做为应用程序来说,它们没有办法知道服务器在未来的请求中将如何回应。

POST方式和GET方法的最大区别就是把发送的数据和URI地址分离。请求参数是在http标题的一个不同部分(名为entity body)传输的,这一部分用来传输表单信息,因此必须将Content-type设置为:application/x-www-form- urlencoded。

Post请求格式如下:

POST /login.asp HTTP/1.1

Accept: */*

Referer: http://www.wantsoft.com

Accept-Language: zh-cn,en-us;q=0.5

Content-Type: application/x-www-form-urlencoded

User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; .NET CLR 1.0.3705; .NET CLR 1.1.4322)

Host: www.wantsoft.com

Content-Length: 35

Pragma: no-cache

Cache-Control: no-cache

username=wantsoft&password=password   //post的数据

  php的post请求的sample如下:  

1: <;?php      
2:   $srv_ip = '127.0.0.1';//你的目标服务地址或频道.      
3:   $srv_port = 80;      
4:   $url = '/helloworld/?r=helloworld'; //接收你post的URL具体地址       
5:   $fp = '';      
6:   $resp_str = '';      
7:   $errno = 0;      
8:   $errstr = '';      
9:   $timeout = 10;      
10:   $post_str = "username=demo&str=aaaa";//要提交的内容.      
11:         
12:   //echo $url_str;      
13:   if ($srv_ip == ''){      
14:    echo('ip or dest url empty<;br>');      
15:   }      
16:   //echo($srv_ip);      
17:   $fp = fsockopen($srv_ip,$srv_port,$errno,$errstr,$timeout);      
18:   if (!$fp){      
19:    echo('fp fail');      
20:   }      
21:   $content_length = strlen($post_str);      
22:   $post_header = "POST $url HTTP/1.1\r\n";      
23:   $post_header .= "Content-Type: application/x-www-form-urlencoded\r\n";      
24:   $post_header .= "User-Agent: MSIE\r\n";      
25:   $post_header .= "Host: ".$srv_ip."\r\n";      
26:   $post_header .= "Content-Length: ".$content_length."\r\n";      
27:   $post_header .= "Connection: close\r\n\r\n";      
28:   $post_header .= $post_str."\r\n\r\n";      
29:   fwrite($fp,$post_header);      
30:   while(!feof($fp)){      
31:    $resp_str .= fgets($fp,512);//返回值放入$resp_str      
32:   }      
33:   fclose($fp);      
34:   echo($resp_str);//处理返回值.      
35:   //unset ($resp_str);      
36: ?>      

获取POST请求

一般我们都用$_POST或$_REQUEST两个预定义变量来接收POST提交的数据。但如果提交的数据没有变量名,而是直接的字符串,则需要使用其他的方式来接收。

方法一: 使用全局变量$GLOBALS['HTTP_RAW_POST_DATA']来获取。

      在$GLOBALS['HTTP_RAW_POST_DATA']存放的是POST过来的原始数据。而$_POST或$_REQUEST存放的是PHP以key=>value的形式格式化以后的数据。 但$GLOBALS['HTTP_RAW_POST_DATA']中是否保存POST过来的数据取决于centent-Type的设置,即POST数据时必须显式示指明Content-Type: application/x-www-form-urlencoded,POST的数据才会存放到 $GLOBALS['HTTP_RAW_POST_DATA']中。(该部分需要php.ini中设置 always_populate_raw_post_data = On 才能行的通)

方法二: 使用file_get_contents("php://input")来获取。

     对于未指定 Content-Type 的POST数据,则可以使用file_get_contents("php://input");来获取原始数据。事实上,用PHP接收POST的任何数据都可以使用本方法。而不用考虑Content-Type,包括二进制文件流也可以。 所以用方法二是最保险的方法。

1: echo "<br> Post Data is :<br>";      
2:   foreach($_POST as $key=>$val)      
3:   {      
4:       echo "key:$key,val:$val<br>";      
5:   }      
6:         
7:   $data = file_get_contents("php://input");      
8:   echo $data.'<br>';      
9:   echo $GLOBALS['HTTP_RAW_POST_DATA'];      

结果如下: