下面的程序可以通过 telnet 进行模拟

<?php

//post数据
$data = rawurlencode("name").'='.rawurlencode("Allen Hu");

//url,支持从命令行获取
$url = $argv[1]?$argv[1]:'https://www.cnbeta.com/index.htm';
$urlinfo = parse_url($url);

//打开连接
$fp = fsockopen($urlinfo['host'], $urlinfo['port']?$urlinfo['port']:80, $errno, $errstr, 10);

//发送数据
fwrite($fp, "POST {$urlinfo['path']}{$urlinfo['query']} HTTP/1.0\r\n");
fwrite($fp, "Host: {$urlinfo['host']}\r\n");
fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fwrite($fp, "Content-Length: ".strlen($data)."\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n");
fwrite($fp, "$data\r\n");

//读取数据
while(!feof($fp)){
    $resp .= fread($fp, 1024);
}

//关闭连接
fclose($fp);

//拆分获取到的数据,输出
list($resp_header,$resp_body) = explode("\r\n\r\n",$resp);
echo $resp_body;

Post Navigation