php 发送和接受自动http请求的实例分享

发布时间:2020-02-07编辑:脚本学堂
本文介绍下,php实现发送与接受自动http请求的一个例子,供大家学习参考。

php发送与接受http自动请求,使用socket操作,http自定义herader。;

1,文件 1.php

<?php
$A=trim(urlencode('aaaa'));
$B=trim(urlencode('bbbb'));
$C=trim(urlencode('ccccc'));
$params = "Content-A=$A&Content-B=$B&Content-C=$C";

$length = strlen($params);
$fp = fsockopen("localhost",80,$errno,$errstr,10) or exit($errstr."--->".$errno);
$header  = "POST /2.php HTTP/1.1rn";//一般有post, get这两种 
$header .= "Host:localhostrn"; 
$header .= "Referer:1.phprn"; 
$header .= "Content-Type: application/x-www-form-urlencodedrn"; 
$header .= "Content-Length: ".$length."rn"; 
$header .= "Connection: Closernrn"; 
$header .= $params."rn";

fputs($fp,$header);
$inheader = 1; 
    while (!feof($fp)) { 
        $line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据 
        if ($inheader && ($line == "n" || $line == "rn")) { 
             $inheader = 0; 
        } 
        if ($inheader == 0) { 
          echo $line; 
        } 
    } 
 
fclose($fp); 
//by www.jb200.com 
?>

2,文件 2.php

<?php
echo "this is the data posted"; 
echo "<pre>"; 
print_r($_REQUEST); 
echo "</pre>";

foreach (getallheaders() as $name => $value) {
    echo "$name: $valuen";
}
/*function GetHeaderInfo(){ 
    foreach ($_SERVER as $name => $value){
        $headers[$name] = $value; 
    } 
    return $headers; 
}
var_dump(GetHeaderInfo());*/
?>