socket

来源:互联网 发布:excel导入oracle数据库 编辑:程序博客网 时间:2024/05/16 10:35
<?phpinterface Proto{    function post();    function get();    function conn($url);    function close();}class Http implements Proto{    protected $version= 'HTTP/1.1';    protected $line   = array();    protected $header = array();    protected $body   = array();    protected $url    = array();    protected $fh     = null;    protected $errorno  = -1;    protected $errorstr='';    protected $response='';    const  CRLF       = "\r\n";    public function __construct($url){        $this->conn($url);        $this->setHeader('Host: '.$this->url['host']);    }    //请求行    protected function setLine($method){        $this->line[0] = $method.' '.$this->url['path'].' '.$this->version;    }    //头信息    protected function setHeader($headerline){        $this->header[] = $headerline;    }    //主体    protected function setBody(){}    //连接URL    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->errorno,$this->errorstr,3        );    }    //构造get查询    public function get(){        $this->setLine('GET');        $this->request();        return $this->response;    }    //构造POST查询    public function post(){        $this->setLine('POST');    }    //请求    public function request(){        $req = array_merge($this->line,$this->header,array(''),$this->body,array(''));        $req = implode(self::CRLF,$req);        fwrite($this->fh,$req);        while(!feof($this->fh)){            $this->response .= fread($this->fh,1024);        }        $this->close();    }    //close    public function close(){    }}$url = 'http://localhost/index.php';$http = new Http($url);echo $http->get();

post

<?phpinterface Proto{    function post();    function get();    function conn($url);    function close();}class Http implements Proto{    protected $version= 'HTTP/1.1';    protected $line   = array();    protected $header = array();    protected $body   = array();    protected $url    = array();    protected $fh     = null;    protected $errorno  = -1;    protected $errorstr='';    protected $response='';    const  CRLF       = "\r\n";    public function __construct($url){        $this->conn($url);        $this->setHeader('Host: '.$this->url['host']);    }    //请求行    protected function setLine($method){        $this->line[0] = $method.' '.$this->url['path'].' '.$this->version;    }    //头信息    protected function setHeader($headerline){        $this->header[] = $headerline;    }    //主体    protected function setBody($body){        $this->body[] = http_build_query($body);    }    //连接URL    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->errorno,$this->errorstr,3        );    }    //构造get查询    public function get(){        $this->setLine('GET');        $this->request();        return $this->response;    }    //构造POST查询    public function post($body = array()){        $this->setLine('POST');        $this->setBody($body);        $this->setHeader('Content-type: application/x-www-form-urlencoded');        $this->setHeader('Content-length: '.strlen($this->body[0]));        $this->request();    }    //请求    public function request(){        $req = array_merge($this->line,$this->header,array(''),$this->body,array(''));        $req = implode(self::CRLF,$req);        fwrite($this->fh,$req);        while(!feof($this->fh)){            $this->response .= fread($this->fh,1024);        }        $this->close();    }    //close    public function close(){    }}$url = 'http://localhost/index.php';$http = new Http($url);echo $http->post();
0 0
原创粉丝点击