- <?php
- $srv_ip = '192.168.1.5';//你的目标服务地址.
- $srv_port = 80;//端口
- $url = 'http://localhost/fsock.php'; //接收你post的URL具体地址
- $fp = '';
- $errno = 0;//错误处理
- $errstr = '';//错误处理
- $timeout = 10;//多久没有连上就中断
- $post_str = "username=demo&password=hahaha";//要提交的内容.
- //打开网络的 Socket 链接。
- $fp = fsockopen($srv_ip,$srv_port,$errno,$errstr,$timeout);
- if (!$fp){
- echo('fp fail');
- }
- $content_length = strlen($post_str);
- $post_header = "POST $url HTTP/1.1\r\n";
- $post_header .= "Content-Type: application/x-www-form-urlencoded\r\n";
- $post_header .= "User-Agent: MSIE\r\n";
- $post_header .= "Host: ".$srv_ip."\r\n";
- $post_header .= "Content-Length: ".$content_length."\r\n";
- $post_header .= "Connection: close\r\n\r\n";
- $post_header .= $post_str."\r\n\r\n";
- fwrite($fp,$post_header);
- $inheader = 1;
- while(!feof($fp)){//测试文件指针是否到了文件结束的位置
- $line = fgets($fp,1024);
- //去掉请求包的头信息
- if ($inheader && ($line == "\n" || $line == "\r\n")) {
- $inheader = 0;
- }
- if ($inheader == 0) {
- echo $line;
- }
- }
- fclose($fp);
- unset ($line);
- ?>
简要说明:代码第二行是你的IP地址或域名,第四行是你要POST的页面的具体地址,本例用的是fsock.php,fsock.php内容如下:
- <?php
- echo "username:".$_POST['username']."<br/>";
- echo "password:".$_POST['password'];
- ?>
结果为:
username:demo
password:hahaha