发送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的数据
1: <;?php
2: $srv_ip &#61; &#39;127.0.0.1&#39;;//你的目标服务地址或频道.
3: $srv_port &#61; 80;
4: $url &#61; &#39;/helloworld/?r&#61;helloworld&#39;; //接收你post的URL具体地址
5: $fp &#61; &#39;&#39;;
6: $resp_str &#61; &#39;&#39;;
7: $errno &#61; 0;
8: $errstr &#61; &#39;&#39;;
9: $timeout &#61; 10;
10: $post_str &#61; "username&#61;demo&str&#61;aaaa";//要提交的内容.
11:
12: //echo $url_str;
13: if ($srv_ip &#61;&#61; &#39;&#39;){
14: echo(&#39;ip or dest url empty<;br>&#39;);
15: }
16: //echo($srv_ip);
17: $fp &#61; fsockopen($srv_ip,$srv_port,$errno,$errstr,$timeout);
18: if (!$fp){
19: echo(&#39;fp fail&#39;);
20: }
21: $content_length &#61; strlen($post_str);
22: $post_header &#61; "POST $url HTTP/1.1\r\n";
23: $post_header .&#61; "Content-Type: application/x-www-form-urlencoded\r\n";
24: $post_header .&#61; "User-Agent: MSIE\r\n";
25: $post_header .&#61; "Host: ".$srv_ip."\r\n";
26: $post_header .&#61; "Content-Length: ".$content_length."\r\n";
27: $post_header .&#61; "Connection: close\r\n\r\n";
28: $post_header .&#61; $post_str."\r\n\r\n";
29: fwrite($fp,$post_header);
30: while(!feof($fp)){
31: $resp_str .&#61; fgets($fp,512);//返回值放入$resp_str
32: }
33: fclose($fp);
34: echo($resp_str);//处理返回值.
35: //unset ($resp_str);
36: ?>
获取POST请求
一般我们都用$_POST或$_REQUEST两个预定义变量来接收POST提交的数据。但如果提交的数据没有变量名&#xff0c;而是直接的字符串&#xff0c;则需要使用其他的方式来接收。
方法一&#xff1a; 使用全局变量$GLOBALS[&#39;HTTP_RAW_POST_DATA&#39;]来获取。
在$GLOBALS[&#39;HTTP_RAW_POST_DATA&#39;]存放的是POST过来的原始数据。而$_POST或$_REQUEST存放的是PHP以key&#61;>value的形式格式化以后的数据。 但$GLOBALS[&#39;HTTP_RAW_POST_DATA&#39;]中是否保存POST过来的数据取决于centent-Type的设置&#xff0c;即POST数据时必须显式示指明Content-Type: application/x-www-form-urlencoded&#xff0c;POST的数据才会存放到 $GLOBALS[&#39;HTTP_RAW_POST_DATA&#39;]中。(该部分需要php.ini中设置 always_populate_raw_post_data &#61; On 才能行的通)
方法二&#xff1a; 使用file_get_contents("php://input")来获取。
对于未指定 Content-Type 的POST数据&#xff0c;则可以使用file_get_contents("php://input");来获取原始数据。事实上&#xff0c;用PHP接收POST的任何数据都可以使用本方法。而不用考虑Content-Type,包括二进制文件流也可以。 所以用方法二是最保险的方法。
1: echo "
Post Data is :
";
2: foreach($_POST as $key&#61;>$val)
3: {
4: echo "key:$key,val:$val
";
5: }
6:
7: $data &#61; file_get_contents("php://input");
8: echo $data.&#39;
&#39;;
9: echo $GLOBALS[&#39;HTTP_RAW_POST_DATA&#39;];
结果如下&#xff1a;