php socket 模拟post,用PHP的Socket编程模拟Post来提交数据 | 学步园
利用php的socket編程來直接給接口發(fā)送數(shù)據(jù)來模擬post的操作。
其實主要用在小偷程序上,也就是采集.
他的核心程序主是用header函數(shù)來偽造發(fā)送http頭信息
$flag = 0;
/*要post的數(shù)據(jù)*/
$argv = array(
'var1'=>'abc',
'var2'=>'你好PHP程序員站');
/*構(gòu)造要post的字符串*/
foreach ($argv as $key=>$value) {
if ($flag!=0) {
$params .= "&"; phperz~com
$flag = 1;
}
$params.= $key."=";
$params.= urlencode($value);
$flag = 1;
}
$length = strlen($params);
/*創(chuàng)建socket連接 */
$fp = fsockopen("127.0.0.1",80,$errno,$errstr,10) or exit($errstr."--->".$errno);
/*構(gòu)造post請求的頭 */
$header = "POST /mobile/try.php HTTP/1.1/r/n"; /*制定為 POST的方法提交數(shù)據(jù) 及要提交到的頁面和協(xié)議類型*/
$header .= "Host:127.0.0.1/r/n"; /*定義主機*/
$header .= "Referer:http://127.0.0.1/mobile/sendpost.php/r/n";
/*Referer信息,如果提交到的地址和當前不在一個域內(nèi),并且遠程主機起用了防盜鏈,此值就很重要,必須為遠程主機的域名 */
$header .= "Content-Type: application/x-www-form-urlencoded/r/n"; /*說明這個請求為POST*/
$header .= "Content-Length: ".$length."/r/n"; /*提交的數(shù)據(jù)長度*/
$header .= "Connection: Close/r/n/r/n"; /*關(guān)閉連接:注意.此處必須得有四個回車, 或著在下面post的數(shù)據(jù)前加上二個加車 /r/n */
$header .= $params."/r/n"; /*添加post的字符串 */
/*發(fā)送post的數(shù)據(jù) */
fputs($fp,$header);
$inheader = 1;
while (!feof($fp)) {
$line = fgets($fp,1024); /*去除請求包的頭只顯示頁面的返回數(shù)據(jù) */
if ($inheader && ($line == "/n" || $line == "/r/n")) {
$inheader = 0;
}
if ($inheader == 0) {
echo $line;
}
}
fclose($fp); /*關(guān)閉socket連接
?>
==============================================
==============================================
總結(jié)
以上是生活随笔為你收集整理的php socket 模拟post,用PHP的Socket编程模拟Post来提交数据 | 学步园的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java线程池与Lambda表达式
- 下一篇: 烟台初中计算机会考,山东烟台市2018年