socket

<?php
interface 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

<?php
interface 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值