下面代码是接收数据的。可以在控制台敲入下面注释起来的代码,模拟表单提交用户名和密码,则执行完毕会在post1.txt中输出文本:xxj(换行)123
<?php
/****
telnet localhost 80
POST /http/41/20140604_1.php HTTP/1.1
Host:localhost//要请求的主机
Content-length:20 //必须有 请求主体的长度
Content-type:application/x-www-form-urlencoded //必须有
username=xxj&pwd=123 //请求主体信息
****/
$str = implode($_POST,"\r\n"); //这样写回车换行就起作用 如果把双引号写成单引号则不行$str = implode($_POST,‘\r\n’); 错误
file_put_contents('./post1.txt', $str);
print_r($str);
?>
POST比GET头信息多了content-type和content-length.
利用telnet完成post请求,不用浏览器发送请求
领悟:
1.使用telnet打开服务器(localhost)的80端口;(telnet localhost 80)
2.用控制台命令向该端口发送请求:
(POST /http/41/20140604_1.php HTTP/1.1
Host:localhost//要请求的主机
Content-length:20 //必须有
Content-type:application/x-www-form-urlencoded //必须有
username=xxj&pwd=123 //请求主体信息
)
3.服务器(localhost)上存有处理请求信息的.php文件:
<?
$str = implode($_POST,"\r\n"); //这样写回车换行就起作用 如果把双引号写成单引号则不行$str = implode($_POST,‘\r\n’); 错误
file_put_contents('./post1.txt', $str);
print_r($str);
?>
4.处理完之后将信息反馈回来:
HTTP/1.1 200 OK
Date: Fri, 13 Jun 2014 06:29:28 GMT
Server: Apache/2.2.22 (Win32) PHP/5.3.13
X-Powered-By: PHP/5.3.13
Content-Length: 10 //返回主体的长度
Content-Type: text/html
xxj
123