PHP + Socket Programming Practice-Blog reply

Source: Internet
Author: User
Because it is also the way to learn, first draw ideas, code and look at other people's learning, the last is based on the idea of completion, may be relatively simple, late in to improve and change     <?php/**  * php+socket programming HTTP request  * @version  c.php  * Mock Blog Park reviews  */interface proto{   //connection URL     public functio N Conn ($url);    //Send GET request     Public function get ();    //Send POST request     Public Function post ($body);    //off connection     public function close (); Class Http implements proto{    protected $response = ';     protected $fh =null;     Pro tected $errno =-1;     Protected $errorstr = ';       Protected $line = Array ();     Protected $header = Array ();     Protected $body = Array ();     Public $url = Array ();       Public function __construct ($url) {        $this->conn ($url);     &N Bsp   $this->setheader (' Host: '. $this->url[' host '));    } &NBsp  //Request line     protected function Setline ($method) {        $this->line[0] = $method. ' ' . $this->url[' path '].   '. ' http/1.1 ';    }    //header information     public function SetHeader ($headerline) {        $th Is->header[] = $headerline;    }    //main information     protected function Setbody ($body) {                $this->body[] = http_build_query ($body);    }    //connection URL     PUBLIC function conn ($url) {        $this->url = Parse_url ($url);         if (!isset ($this->url[' Port ')) {            $this->url[' p Ort '] = 80;        }         $this->fh = fsockopen ($this->url[' host '), $this->url[' p Ort '], $this->errno, $this->errorstr,3);    }    //Build GET request     Public function get () {        $this->setline (' get ');         $this->request ();         return $this->response;    }    //Send POST request     Public Function post ($BO) {        $this->s Etline (' POST ');                //content-type               & nbsp $this->setheader ("content-type:application/x-www-form-urlencoded");                   $this->setbody ($BO);                //content-length                 $this->setheader ("Content-length:" strlen ($this->body[0));                 $this->request ();                 return $this->response;    } &NBsp   Public Function request () {        $req = Array_merge ($this->line, $this->header,array (' '), $this->body,array ("));        //print_r ($req);d ie;         $req = implode (Php_eol, $req); Php_eol/     echo $req;d ie;         fwrite ($this->fh, $req);         while (!feof ($this->fh)) {            $this->response. = fr EAD ($this->fh,1024);        }         $this->close ();    }     Public function close () {            fclose ($this->fh) &nb Sp  }   above is a simple class     <?php   require ("./c.php"); Introducing the above class $url = "Http://www.cnblogs.com/mvc/PostComment/Add.aspx"; $http = new http ($url); $http->setheader ("cookie:xxxx"); $http->setheader ("referer:http://www.cnblogs.com/geek12/p/4024793.html"); $Http->setheader ("user-agent:mozilla/5.0" (Windows NT 6.3; WOW64; rv:32.0) gecko/20100101 firefox/32.0 ");   $msg =array ("Blogapp" => "geek12", "Body" => "from Robot", "Parentcommentid" =>0, "PostID" =>4024793);   $http->post ($msg);

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.