PHP+SOCKET 類比HTTP請求

來源:互聯網
上載者:User

標籤:web服務   this   技術   ade   http狀態代碼   包含   protected   擷取   技術分享   

HTTP訊息結構

用戶端請求包括四部份:請求行(狀態行)、要求標頭、空行、請求主體(資料),如:

服務端響應包括四部份:響應行(狀態行)、回應標頭、空行、響應主體(資料),

HTTP要求方法:

POST 、GET  、HEADE、  PUT、  TRACE  、DELETE 、OPTIONS 、CONNECT  (前三種最實用),有這麽多的要求方法,但web伺服器不一定所有的都支援。

GET   基本一致,請求指定的頁面資訊,並返回實體主體。

HEAD  基本和GET一致 ,只不過返回的響應中沒有具體的內容,用於擷取前序

POST  向指定資源提交資料進行處理請求(例如表單、上傳檔案)。資料被包含在請求體中。該請求可能會導致新資源的建立或   已有資源的修改

OPTIONS 返回伺服器可用的方法 ,允許用戶端查看伺服器的效能。

PUT  從用戶端向伺服器傳送的資料取代指定的文檔的內容。

DELETE 請求伺服器刪除指定的頁面

CONNECT HTTP/1.1協議中預留給能夠將串連改為管道方式的Proxy 伺服器。

TRACE  回顯伺服器收到的請求,主要用於測試或診斷。

HTTP狀態代碼

HTTP狀態代碼分五類

1xxx    接收到請求,繼續處理

2xxx    請求成功

3xxx   重新導向

4xxx   用戶端錯誤

5xxx  伺服器請求錯誤

常用的常態碼

200          請求成功

301/302             永久/臨時重新導向

304         未修改,從緩衝中進行讀取

404         資源不存在

500         伺服器內部錯誤

307         重新導向中保持原有得資料不丟失

503         伺服器暫時不可用

根據上面的對HTTP有了一定的瞭解,根據對應的請求的方式進行拼接

PHP+SOCKET 類比HTTP請求

介面

** * Interface php_socket */interface php_socket{    /**     * @return mixed     */    public function get();    /**     * @return mixed     */    public function post($param);    /**     * @return mixed     */    public function request();    /**     * @return mixed     */    public function header_info();    /**     * @return mixed     */    public function body_info($param);    /**     * @return mixed     */    public function connect();}

實現介面

class  implement_socket implements php_socket{    protected $http_tpye = ‘HTTP/1.1‘;    protected $url = ‘‘;    protected $request_type = ‘‘;    protected $lines = ‘‘;    protected $fsoket_open = ‘‘;    protected $port = ‘‘;    protected $errstr = ‘‘;    protected $timeout = 0;    protected $parse_url = ‘‘;    protected $content_type = ‘‘;    protected $content_length = 0;    protected $body = ‘‘;    function __construct($url, $request_type = ‘‘, $port = 80, $timeout = 5)    {        $this->url = $url;        $this->request_type = $request_type;        $this->port = $port;        $this->timeout = $timeout;        $this->parse_url = parse_url($url);        //連結        $this->connect();    }    /*     *佈建要求行     * */    public function get()    {        $this->content_type = ‘text/html‘;        $this->lines = $this->request_type . ‘ ‘ . $this->parse_url[‘path‘] . ‘ ‘ . $this->http_tpye;        $this->request();    }    public function post($param)    {        //設定頭資訊        $this->content_type = ‘application/x-www-form-urlencoded‘;        $data = $this->body_info($param);        $this->content_length = strlen($data);        $this->lines = $this->request_type . ‘ ‘ . $this->parse_url[‘path‘] . ‘ ‘ . $this->http_tpye;        $this->body = $data;        $this->request();    }    public function request()    {        $getinfo = ‘‘;        echo $this->lines . "\r\n" . implode("\r\n", $this->header_info()) . " \n\r\n" . $this->body;        exit();        //連結成功進行寫入頭資訊        fwrite($this->fsoket_open, $this->lines . "\n" . implode("\n", $this->header_info()) . "\n\r\n" . $this->body);        while (!feof($this->fsoket_open)) {            $getinfo .= fgets($this->fsoket_open, 1024);        }        fclose($this->fsoket_open);        echo "以下是擷取的資訊:<br>" . $getinfo;    }    /*     * 連結     * */    public function connect()    {        try {            $this->fsoket_open = fsockopen($this->parse_url[‘host‘], $this->port, $this->errstr, $this->timeout);        } catch (Exception $exception) {            echo ‘connect is failed :‘ . $exception->getMessage() . ‘r\n‘ . $this->errstr;        }    }    /*     * 設定頭資訊     * */    public function header_info()    {        return array(            "Host:" . $this->parse_url[‘host‘],            "Content-type:" . $this->content_type,            "Content-length:" . $this->content_length        );    }    /*     * 設定主體     * */    public function body_info($param)    {        // 產生 URL-encode 之後的請求字串        return http_build_query($param);    }}

PHP+SOCKET 類比HTTP請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.