HTTP socket 类

<?php
    interface Proto
    {
        // 连接
        function conn($url);

        // 发送get请求
        function get();

        // 发送post查询
        function post($data);

        // 关闭连接
        function close();
    }

    class Http implements Proto
    {

        const CRLF              = "\r\n";     // 行分隔符
        protected $url          = null;       // url地址
        protected $errno        = -1;         // 错误号
        protected $errstr       = '';         // 错误信息
        protected $response     = '';         // 返回信息
        protected $version      = 'HTTP/1.1'; // HTTP协议版本
        protected $fh           = null;       // 打开远程文件的资源
        protected $line         = array();    // 消息行
        protected $header       = array();    // 请求头
        protected $body         = array();    // 实体信息
        protected $expires_time = 300;          // 连接过期时间

        public function __construct($url)
        {
            $this->conn($url);
            $this->setHeader("Host: ".$this->url['host']);
        } 

        // 连接
        public function conn($url)
        {
            $this->url = parse_url($url);
            if (!isset($this->url['port'])) 
            {
                $this->url['port'] = '80';
            }

            $this->fh = fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,$this->expires_time);
        }

        // 设置请求行
        protected function setLine($method)
        {
            $this->url['path'] .= isset($this->url['query']) ? '?'.$this->url['query'] : '';
            $this->line[] = $method.' '.$this->url['path'].' '.$this->version;
        }

        // 设置头信息
        public function setHeader($header)
        {
            $this->header[] = $header;
        }

        // 设置实体信息
        protected function setBody($data)
        {
            $this->body[] = http_build_query($data);
            // $this->body[0] = $data;
        }

        // 发送get请求
        public function get()
        {
            $this->setLine('GET');
            $this->request();
            return $this->response;
        }

        // 发送post查询
        public function post($data)
        {
            $this->setLine('POST');
            $this->setHeader("Content-type: application/x-www-form-urlencoded");
            $this->setBody($data);
            $this->setHeader("Content-length: ".strlen($this->body[0]));
            $this->request();
            return $this->response;
        }

        // 真正的请求
        protected function request()
        {
            $req = array_merge($this->line,$this->header,array(''),$this->body,array(''));
            $req = implode(self::CRLF,$req);
            echo $req;
            // exit;
            // 写入
            fwrite($this->fh,$req);
            while(!feof($this->fh)){
                $this->response .= fread($this->fh,1024); 
            }
            $this->close();
        }


        // 关闭连接
        public function close()
        {
            fclose($this->fh);
        }
    }
// $url = 'http://shunping.com/msg.php';
// $url = 'http://news.163.com/13/0613/09/9187CJ4C00014JB6.html';
// $http = new Http($url);
// echo $http->get();
// tit=test&con=tset&submit=%E6%8F%90%E4%BA%A4
// $data['tit'] = 'test';
// $data['con'] = 'tset';
// $data['submit'] = '%E6%8F%90%E4%BA%A4'; 
// $data['submit'] = '提交';
// echo $http->post($data);
?>



你可能感兴趣的:(http,socket编程)