PHP socket類

來源:互聯網
上載者:User
沒事的時候自己封裝了一個socket類

功能很簡單和curl功能是一樣的

class socketClass{    private $host;    private $url;    private $error_no = 0;    public $error_str = '';    private $port = 80;    private $timeout = 5;    private $method;    private $param;    private $fp;    private $content = '';    private $header = array();    public function __construct($host, $port, $url, $param, $method='GET', $timeout=5) {        $this->host = $host;        $this->port = $port;        $this->url = $url;        $this->timeout = $timeout;        $this->method = $method;        if(is_array($param)){            $this->param = http_build_query($param);        }else{            $this->param = $param;        }    }    private function connect(){        $this->fp = fsockopen($this->host, $this->port, $this->error_no, $this->error_str, $this->timeout);        if(!$this->fp){            $this->error_str = 'socket connect failed';            return false;        }        return true;    }    private function send(){        $this->fp = '';        if(!$this->connect()){            $this->error_str = 'connect socket failed';            return false;        }        if($this->method == 'POST'){            $header = "POST $this->url HTTP/1.1\r\n";        }else{            if(!empty($this->param)){                $header = "GET $this->url?$this->param HTTP/1.1\r\n";            }else{                $header = "GET $this->url HTTP/1.1\r\n";            }        }        $header .= "Content-Length:" . strlen($this->param) . "\r\n";        $header .= "Host:$this->host:$this->port\r\n";        $header .= "Content-Type:application/x-www-form-urlencode\r\n";        $header .= "Connection:close\r\n\r\n";        if($this->method == 'POST'){            $header .= "$this->param\r\n\r\n";        }        if(!empty($this->header)){            foreach ($this->header as $key=>$val){                $header .= "$key:$val\r\n";            }        }        fwrite($this->fp, $header);        while(!feof($this->fp)){            $this->content .= fread($this->fp, 1024);        }        fclose($this->fp);        return true;    }    public function getContent(){        if(!$this->send()){            return false;        }        $pos = strpos($this->content, "\r\n\r\n");        $this->content = substr($this->content, $pos);        return $this->content;    }    public function setHeader($key, $val){        $this->header[$key] = $val;    }    public function getErrorStr(){        return $this->error_str;    }}$host = 'localhost';$port = 81;$url = 'http://localhost/phpdemo/php/socket_accept.php';$param = array('username'=>'beggar', 'passwd'=>'20060400');$method = 'GET';$timeout = 10;$http = new socketClass($host, $port, $url, $param, $method, $timeout);$http->setHeader('referer', $url);$http->setHeader('Accept', '*/*');$header = $http->getContent();echo $header;

以上就介紹了PHP socket類,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

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