欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用PHP Socket 編程模擬Http post和get請求

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

這里給大家分享一段使用PHP Socket 編程模擬Http post和get請求的代碼,非常的實(shí)用,結(jié)尾部分我們再討論下php模擬http請求的幾種方法。

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

<?php /**
 * 使用PHP Socket 編程模擬Http post和get請求
 * @author koma
 */ class Http{
    private $sp = "\r\n"; //這里必須要寫成雙引號     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對象單例    
    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請求 
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請求 */ 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請求

方法一:利用php的socket編程來直接給接口發(fā)送數(shù)據(jù)來模擬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請求的頭
    $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); //去除請求包的頭只顯示頁面的返回?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)通過測試。

方法二:

使用PHP的curl擴(kuò)展或HttpClient.class.php類,這兩個(gè)非常類似,下面簡單的列出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è)要借助第三方類庫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)文章

  • PHP獲取本周所有日期或者最近七天所有日期的方法

    PHP獲取本周所有日期或者最近七天所有日期的方法

    這篇文章主要介紹了PHP獲取本周所有日期或者最近七天所有日期的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-06-06
  • PHP中的Streams詳細(xì)介紹

    PHP中的Streams詳細(xì)介紹

    這篇文章主要介紹了PHP中的Streams詳細(xì)介紹,本文介紹了Stream 基礎(chǔ)知識、php:// Streams包裝類、Stream上下文等內(nèi)容,需要的朋友可以參考下
    2014-11-11
  • PHP中的使用curl發(fā)送請求(GET請求和POST請求)

    PHP中的使用curl發(fā)送請求(GET請求和POST請求)

    本篇文章主要介紹了PHP中的使用curl發(fā)送請求(GET請求和POST請求),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • CI(CodeIgniter)框架介紹

    CI(CodeIgniter)框架介紹

    CodeIgniter 是一個(gè)為用 PHP 編寫網(wǎng)絡(luò)應(yīng)用程序的人員提供的工具包。它的目標(biāo)是實(shí)現(xiàn)讓你比從零開始編寫代碼更快速地開發(fā)項(xiàng)目,為此,CI 提供了一套豐富的類庫來滿足通常的任務(wù)需求,并且提供了一個(gè)簡單的接口和邏輯結(jié)構(gòu)來調(diào)用這些庫。CodeIgniter 可以將需要完成的任務(wù)代碼量最小化,這樣你就可以把更多的精力放到項(xiàng)目的開發(fā)上了。
    2014-06-06
  • PHP異常處理淺析

    PHP異常處理淺析

    這篇文章主要介紹了PHP異常處理淺析,本文著重講解如何捕獲異常,并給出代碼操作實(shí)例,需要的朋友可以參考下
    2015-05-05
  • yii上傳文件或圖片實(shí)例

    yii上傳文件或圖片實(shí)例

    最近在看yii,yii有自帶上傳圖片的方法。簡單的貼代碼。分為兩塊,第一塊view:test.php。第二塊是controller:TestController.php
    2014-04-04
  • Yii框架數(shù)據(jù)模型的驗(yàn)證規(guī)則rules()被執(zhí)行的方法

    Yii框架數(shù)據(jù)模型的驗(yàn)證規(guī)則rules()被執(zhí)行的方法

    這篇文章主要介紹了Yii框架數(shù)據(jù)模型的驗(yàn)證規(guī)則rules()被執(zhí)行的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-12-12
  • 詳細(xì)解讀PHP的Yii框架中登陸功能的實(shí)現(xiàn)

    詳細(xì)解讀PHP的Yii框架中登陸功能的實(shí)現(xiàn)

    這篇文章主要介紹了詳細(xì)解讀PHP的Yii框架中登陸功能的實(shí)現(xiàn),包括通過cookie實(shí)現(xiàn)自動登錄的功能,需要的朋友可以參考下
    2015-08-08
  • PHP利用DWZ.CN服務(wù)生成短網(wǎng)址

    PHP利用DWZ.CN服務(wù)生成短網(wǎng)址

    這篇文章主要介紹了PHP利用DWZ.CN服務(wù)生成短網(wǎng)址,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。
    2019-08-08
  • PHP之認(rèn)識(二)關(guān)于Traits的用法詳解

    PHP之認(rèn)識(二)關(guān)于Traits的用法詳解

    這篇文章主要介紹了PHP Traits的用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評論