用PHP的Socket编程模拟Post来提交数据
时间:2008-05-29 来源:剑心通明
利用php的socket编程来直接给接口发送数据来模拟post的操作。
其实主要用在小偷程序上,也就是采集.
他的核心程序主是用header函数来伪造发送http头信息.
以下为引用的内容:
?PHP
$flag = 0;
//要post的数据
$argv = array(
www~phperz~com
'var1'=>'abc',
'var2'=>'你好PHP程序员站');
//构造要post的字符串
foreach ($argv as $key=>$value) {
if ($flag!=0) {
$params .= "&"; www.phperz.com
$flag = 1;
}
$params.= $key."="; $params.= urlencode($value);
$flag = 1;
} www.phperz.com
$length = strlen($params);
//创建socket连接
$fp = fsockopen("127.0.0.1",80,$errno,$errstr,10) or exit($errstr."--->".$errno);
www.phperz.com
//构造post请求的头
$header = "POST /mobile/try.php HTTP/1.1\r\n"; //制定为 POST的方法提交数据 及要提交到的页面和协议类型
$header .= "Host:127.0.0.1\r\n"; //定义主机
$header .= "Referer:http://127.0.0.1/mobile/sendpost.php\r\n"; //Referer信息,如果提交到的地址和当前不在一个域内,并且远程主机起用了防盗链,此值就很重要,必须为远程主机的域名
phperz.com
$header .= "Content-Type: application/x-www-form-urlencoded\r\n"; //说明这个请求为POST
$header .= "Content-Length: ".$length."\r\n"; //提交的数据长度
$header .= "Connection: Close\r\n\r\n";//关闭连接:注意.此处必须得有四个回车, 或着在下面post的数据前加上二个加车 \r\n www~phperz~com
$header .= $params."\r\n";//添加post的字符串
//发送post的数据
fputs($fp,$header);
$inheader = 1;
while (!feof($fp)) {
php程序员之家
$line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据
if ($inheader && ($line == "\n" || $line == "\r\n")) {
$inheader = 0; php程序员站
}
if ($inheader == 0) {
echo $line;
}
}
fclose($fp); //关闭socket连接
?>
其实主要用在小偷程序上,也就是采集.
他的核心程序主是用header函数来伪造发送http头信息.
以下为引用的内容:
?PHP
$flag = 0;
//要post的数据
$argv = array(
www~phperz~com
'var1'=>'abc',
'var2'=>'你好PHP程序员站');
//构造要post的字符串
foreach ($argv as $key=>$value) {
if ($flag!=0) {
$params .= "&"; www.phperz.com
$flag = 1;
}
$params.= $key."="; $params.= urlencode($value);
$flag = 1;
} www.phperz.com
$length = strlen($params);
//创建socket连接
$fp = fsockopen("127.0.0.1",80,$errno,$errstr,10) or exit($errstr."--->".$errno);
www.phperz.com
//构造post请求的头
$header = "POST /mobile/try.php HTTP/1.1\r\n"; //制定为 POST的方法提交数据 及要提交到的页面和协议类型
$header .= "Host:127.0.0.1\r\n"; //定义主机
$header .= "Referer:http://127.0.0.1/mobile/sendpost.php\r\n"; //Referer信息,如果提交到的地址和当前不在一个域内,并且远程主机起用了防盗链,此值就很重要,必须为远程主机的域名
phperz.com
$header .= "Content-Type: application/x-www-form-urlencoded\r\n"; //说明这个请求为POST
$header .= "Content-Length: ".$length."\r\n"; //提交的数据长度
$header .= "Connection: Close\r\n\r\n";//关闭连接:注意.此处必须得有四个回车, 或着在下面post的数据前加上二个加车 \r\n www~phperz~com
$header .= $params."\r\n";//添加post的字符串
//发送post的数据
fputs($fp,$header);
$inheader = 1;
while (!feof($fp)) {
php程序员之家
$line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据
if ($inheader && ($line == "\n" || $line == "\r\n")) {
$inheader = 0; php程序员站
}
if ($inheader == 0) {
echo $line;
}
}
fclose($fp); //关闭socket连接
?>
相关阅读 更多 +