php重构HTTP包 获取result

时间:2014-05-08 16:43:06   收藏:0   阅读:347

目的是做一个分发的php脚本,根据客户端的不同需求,分发到不同的功能脚本。所以需要首先检验客户端发来的HTTP包中的请求类型,然后再进行分发。

 

分发脚本:

<?php
/*
* This file is to distribute the requests  to different servers , is divided by functions
* Para : Http-Request
* Data : 2014.5.5
*/
// include the mapping-array
include ‘./mapping/RequestType.php‘;
include ‘./mapping/MappingTable.php‘;
// Get functionName from clients
$functionName = $_POST[‘functionName‘];
// include the ReBuildHttpBag function
include ‘ReBuildHttpBag.php‘;
// Generate the para needed
$curPageName = "/WinPhone/Distribute.php";
$desPageName = $mapping[$functionName][‘pageName‘];
$serverPath = $mapping[$functionName][‘serverPath‘];
$serverPort = 80;
ReBuildHttpBag($_GET, $curPageName, $desPageName, $serverPath, $serverPort);
?>

 

ReBuildHttpBag定义:

<?php
function ReBuildHttpBag($argv=array(), $curPageName, $desPageName, $serverPath , $serverPort)
{
$flag = 0;
$post = ‘‘;
$errno = ‘‘;
$errstr = ‘‘;
//构造要post的字符串
foreach ($argv as $key=>$value) {
if ($flag!=0) {
$post .= "&";
$flag = 1;
}
$post.= $key."=";
$post.= urlencode($value);
$flag = 1;
}
$length = strlen($post);
//创建socket连接
$fp = fsockopen($serverPath,$serverPort,$errno,$errstr,10) or exit($errstr."--->".$errno);
//构造post请求的头
$header  = "POST ".$desPageName." HTTP/1.1\r\n";
$header .= "Host:".$serverPath."\r\n";
$header .= "Referer: ".$curPageName." \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";
//添加post的字符串
$header .= $post."\r\n";
//发送post的数据
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);
}
?>

 

php重构HTTP包 获取result,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!