PHP Socket Class

Source: Internet
Author: User
Tags connect socket socket connect
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; The private Function connect () {$this->FP = fsockopen ($this->host, $this->port, $this->error_n
        O, $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) {$TMEP = u Rlencode ($val);
              $header. = "$key: $temp \ r \ n";
        } fwrite ($this->fp, $header);
        while (!feof ($this->fp)) {$this->content. = Fread ($this->fp, 1024);
        } fclose ($this->FP);
    return true;
        The 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;
    The Public Function SetHeader ($key, $val) {$this->header[$key] = $val;
    The 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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.