php重構HTTP包 擷取result

來源:互聯網
上載者:User

標籤:php   http   分發   

目的是做一個分發的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-arrayinclude ‘./mapping/RequestType.php‘;include ‘./mapping/MappingTable.php‘;// Get functionName from clients$functionName = $_POST[‘functionName‘];// include the ReBuildHttpBag functioninclude ‘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定義:

<?phpfunction 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);}?>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.