目的是做一個分發的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); } ?>