zoukankan      html  css  js  c++  java
  • PHP+SOCKET 模拟HTTP请求

    HTTP消息结构

    客户端请求包括四部份:请求行(状态行)、请求头、空行、请求主体(数据),如下图:

    image

    服务端响应包括四部份:响应行(状态行)、响应头、空行、响应主体(数据),如图:

    image

    HTTP请求方法:

    POST 、GET  、HEADE、  PUT、  TRACE  、DELETE 、OPTIONS 、CONNECT  (前三种最实用),有这麽多的请求方法,但web服务器不一定所有的都支持。

    GET   基本一致,请求指定的页面信息,并返回实体主体。

    HEAD  基本和GET一致 ,只不过返回的响应中没有具体的内容,用于获取报头

    POST  向指定资源提交数据进行处理请求(例如表单、上传文件)。数据被包含在请求体中。该请求可能会导致新资源的建立或   已有资源的修改

    OPTIONS 返回服务器可用的方法 ,允许客户端查看服务器的性能。

    PUT  从客户端向服务器传送的数据取代指定的文档的内容。

    DELETE 请求服务器删除指定的页面

    CONNECT HTTP/1.1协议中预留给能够将连接改为管道方式的代理服务器。

    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 . "
    " . implode("
    ", $this->header_info()) . " 
    
    " . $this->body;
            exit();
            //链接成功进行写入头信息
            fwrite($this->fsoket_open, $this->lines . "
    " . implode("
    ", $this->header_info()) . "
    
    " . $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
    ' . $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);
        }
    }
  • 相关阅读:
    opencv访问图像像素
    利用chrome浏览器的proxy switchy扩展智能切换代理
    一些linux命令【ubuntu】
    如何从 Ubuntu 10.04 升级到 10.10
    关于“无法获得排它锁 ”的解决办法
    【ubuntu】Grub2配置详解(转)
    BibTeX使用介绍
    【ubuntu】imagemagick用法
    ubuntu环境下安装OpenCV
    安装wordpress
  • 原文地址:https://www.cnblogs.com/webph/p/6536311.html
Copyright © 2011-2022 走看看