教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php 发送和接受自动http请求的实例分享

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

发布时间:2016-09-30   编辑:jiaochengji.com
本文介绍下,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.1\r\n";//一般有post, get这两种 
$header .= "Host:localhost\r\n"; 
$header .= "Referer:1.php\r\n"; 
$header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
$header .= "Content-Length: ".$length."\r\n"; 
$header .= "Connection: Close\r\n\r\n"; 
$header .= $params."\r\n";

fputs($fp,$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); 
//by www.jbxue.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: $value\n";
}
/*function GetHeaderInfo(){ 
    foreach ($_SERVER as $name => $value){
        $headers[$name] = $value; 
    } 
    return $headers; 
}
var_dump(GetHeaderInfo());*/
?>

您可能感兴趣的文章:
php 发送和接受自动http请求的实例分享
Laravel 开启跨域功能示例
PHP发送POST请求的常用方式
PHPmailer邮件群发的入门例子
php 文件头部(header)信息详解
php模拟多线程 异步执行的实例分享
php curl模拟post请求的例子
flask app python是什么
PHPMailer发送邮件的实例分享
phpmailer发送yahoo邮件的例子

关键词: http请求   
[关闭]
~ ~