最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用PHP Socket 編程模擬Http post和get請(qǐng)求

 更新時(shí)間:2014年11月25日 09:27:56   投稿:hebedich  
這篇文章主要介紹了使用PHP Socket 編程模擬Http post和get請(qǐng)求 ,需要的朋友可以參考下

這里給大家分享一段使用PHP Socket 編程模擬Http post和get請(qǐng)求的代碼,非常的實(shí)用,結(jié)尾部分我們?cè)儆懻撓聀hp模擬http請(qǐng)求的幾種方法。

復(fù)制代碼 代碼如下:

<?php /**
 * 使用PHP Socket 編程模擬Http post和get請(qǐng)求
 * @author koma
 */ class Http{
    private $sp = "\r\n"; //這里必須要寫(xiě)成雙引號(hào)     private $protocol = 'HTTP/1.1';
    private $requestLine = "";
    private $requestHeader = "";
    private $requestBody = "";
    private $requestInfo = "";
    private $fp = null;
    private $urlinfo = null;
    private $header = array();
    private $body = "";
    private $responseInfo = "";
    private static $http = null; //Http對(duì)象單例    
    private function __construct() {}
    public static function create() {
        if ( self::$http === null ) { 
            self::$http = new Http();
        }
        return self::$http;
    }
    public function init($url) {
        $this->parseurl($url);
        $this->header['Host'] = $this->urlinfo['host'];
        return $this;
    }
    public function get($header = array()) {
        $this->header = array_merge($this->header, $header);
        return $this->request('GET');
    }
    public function post($header = array(), $body = array()) {
        $this->header = array_merge($this->header, $header);
        if ( !empty($body) ) {
            $this->body = http_build_query($body);
            $this->header['Content-Type'] = 'application/x-www-form-urlencoded';
            $this->header['Content-Length'] = strlen($this->body);
        }
        return $this->request('POST');
    }
    private function request($method) {
        $header = "";
        $this->requestLine = $method.' '.$this->urlinfo['path'].'?'.$this->urlinfo['query'].' '.$this->protocol;
        foreach ( $this->header as $key => $value ) {
            $header .= $header == "" ? $key.':'.$value : $this->sp.$key.':'.$value;
        }
        $this->requestHeader = $header.$this->sp.$this->sp;
        $this->requestInfo = $this->requestLine.$this->sp.$this->requestHeader;
        if ( $this->body != "" ) {
            $this->requestInfo .= $this->body;
        }
        /*
         * 注意:這里的fsockopen中的url參數(shù)形式為"          * 不能夠帶"http://"這種
         */
        $port = isset($this->urlinfo['port']) ? isset($this->urlinfo['port']) : '80';
        $this->fp = fsockopen($this->urlinfo['host'], $port, $errno, $errstr);
        if ( !$this->fp ) {
            echo $errstr.'('.$errno.')';
            return false;
        }
        if ( fwrite($this->fp, $this->requestInfo) ) {
            $str = "";
            while ( !feof($this->fp) ) {
                $str .= fread($this->fp, 1024);
            }
            $this->responseInfo = $str;
        }
        fclose($this->fp);
        return $this->responseInfo;
    }
    private function parseurl($url) {
        $this->urlinfo = parse_url($url);
    }
} // $url = "
$url = "http://localhost/httppro/post.php"; $http = Http::create()->init($url); /* 發(fā)送get請(qǐng)求 
echo $http->get(array(
    'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36',
));
*/
 /* 發(fā)送post請(qǐng)求 */ echo $http->post(array(
        'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36',
), array('username'=>'發(fā)一個(gè)中文', 'age'=>22));

php 模擬 http請(qǐng)求

方法一:利用php的socket編程來(lái)直接給接口發(fā)送數(shù)據(jù)來(lái)模擬post的操作。

建立兩個(gè)文件post.php,getpost.php
post.php內(nèi)容如下:

復(fù)制代碼 代碼如下:

<?php
 $flag = 0;
 $params = '';
 $errno = '';
 $errstr = '';
 //要post的數(shù)據(jù)
$argv = array(
    'var1'=>'abc',
    'var2'=>'how are you , my friend??');
//構(gòu)造要post的字符串
foreach ($argv as $key=>$value) {
    if ($flag!=0) {
        $params .= "&";
        $flag = 1;
    }
    $params.= $key."="; $params.= urlencode($value);
    $flag = 1;
    }
    $length = strlen($params);
     //創(chuàng)建socket連接
    $fp = fsockopen("localhost",81,$errno,$errstr,10) or exit($errstr."--->".$errno);
    //構(gòu)造post請(qǐng)求的頭
    $header  = "POST /flandy/getpost.php HTTP/1.1\r\n";
    $header .= "Host:127.0.0.1\r\n";
    $header .= "Referer:/flandy/post.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";
    //添加post的字符串
    $header .= $params."\r\n";
   

    //發(fā)送post的數(shù)據(jù)
    fputs($fp,$header);
    $inheader = 1;
    while (!feof($fp)) {
        $line = fgets($fp,1024); //去除請(qǐng)求包的頭只顯示頁(yè)面的返回?cái)?shù)據(jù)
        if ($inheader && ($line == "\n" || $line == "\r\n")) {
             $inheader = 0;
        }
        if ($inheader == 0) {
          echo $line;
        }
    }

fclose($fp);
?>

getpost.php的內(nèi)容如下

復(fù)制代碼 代碼如下:

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

結(jié)果輸出:

復(fù)制代碼 代碼如下:

this is the data postedArray
(
[var1] => abc
[var2] => how are you , my friend??
)

以上代碼在本機(jī)81端口下已經(jīng)通過(guò)測(cè)試。

方法二:

使用PHP的curl擴(kuò)展或HttpClient.class.php類,這兩個(gè)非常類似,下面簡(jiǎn)單的列出curl的實(shí)現(xiàn)代碼。
兩個(gè)文件post2.php和getpost2.php
post2.php的內(nèi)容如下:

復(fù)制代碼 代碼如下:

<?php
$psecode = 'NDE005';
$website = 'www.baidu.com';
$amt = 1;
$pwd = 123456;
$ch = curl_init();
$curl_url = "http://localhost:81/flandy/getpost2.php?web=" . $website .
"&pwd=" . $pwd . "&action=check&pseid=" . $psecode .
"&amt=" . $amt;
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//不直接輸出,返回到變量
$curl_result = curl_exec($ch);
$result = explode(',', $curl_result);
curl_close($ch);
print_r($result);
?>

getpost2.php的內(nèi)容如下:

復(fù)制代碼 代碼如下:

<?php
echo "returndata<br>";
echo "<pre>";
print_r($_REQUEST);
echo "</pre>";
?>

結(jié)果輸出:

復(fù)制代碼 代碼如下:

Array ( [0] => returndataArray
(
[web] => 'wwwbaiducom'
[pwd] => 123456
[action] => check
[pseid] => 'NDE005'
[amt] => 1
)
)

方法三:

這個(gè)要借助第三方類庫(kù)HttpClient可以到這里下載:http://scripts.incutio.com/httpclient/

復(fù)制代碼 代碼如下:

<?php
require_once 'HttpClient.class.php';
$params = array('web' => 'www.baidu.com',
'pwd' => '123456',
'action' => 'check',
'pseid' => 'NDE005',
'amt' => 1);
$pageContents = HttpClient::quickPost('http://localhost:81/flandy/getpost3.php', $params);
$result = explode(',', $pageContents);
print_r($result);
?>

相關(guān)文章

最新評(píng)論

克什克腾旗| 安平县| 凭祥市| 社会| 贺州市| 日喀则市| 垦利县| 新泰市| 苍溪县| 大埔区| 兴宁市| 凤庆县| 河南省| 五原县| 从江县| 焦作市| 宣武区| 贵阳市| 巴中市| 吉水县| 焉耆| 甘德县| 略阳县| 皋兰县| 连城县| 城固县| 襄樊市| 太白县| 永春县| 米易县| 曲阳县| 济南市| 美姑县| 宜兰市| 津市市| 维西| 宜黄县| 布拖县| 策勒县| 运城市| 漳州市|