Because it is also the way of learning, first draw ideas, code and look at the learning of others, the final is based on the idea of completion, may be relatively simple, late in to improve and change
<?php/** * Php+socket Program send HTTP request * @version c.php * Mock Blog Park comment */interface proto{//connection urlpublic function conn ($url);//Hair Send GET request public function get ()//Send POST request public Function post ($body);//close connection Public function close ();} Class Http implements proto{protected $response = ';p rotected $fh =null;protected $errno = -1;protected $errorstr = ';p ro tected $line = Array ();p rotected $header = Array ();p rotected $body = Array ();p ublic $url = Array ();p ublic function __const Ruct ($url) {$this->conn ($url); $this->setheader (' Host: '. $this->url[' host ');} Request line protected function Setline ($method) {$this->line[0] = $method. ‘ ‘ . $this->url[' path ']. ‘ ‘ . ' http/1.1 ';} Header information Public function SetHeader ($headerline) {$this->header[] = $headerline;} Principal information protected function Setbody ($body) {$this->body[] = http_build_query ($body);} Connect urlpublic 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->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->setline (' post '); Content-type $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;} 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 line//echo $req;d ie;fwrite ($this->fh, $req), while (!feof ($this->fh)) {$this->response. = Fread ($ this->fh,1024);} $this->close ();} Public function Close () {fclose ($this->fh);}}
The above is a simple class
<?phprequire ("./c.php"); Introduce the above class: "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 "and" geek12 "," Body "and" from Robot "," Parentcommentid "= >0, "PostID" =>4024793); $http->post ($msg);
Send a successful
In the process of writing a few problems encountered, according to the URL of the request is not the same, the request header stitching also needs to change, here the requested URL is http://www.cnblogs.com/mvc/PostComment/Add.aspx static connection, all path Directly to the back, if the dynamic needs to be modified, so this method is only suitable for static connection, dynamic needs to modify the request header
Not all sites can be simulated, although there is no verification code, I VERYCD site, how the simulation did not succeed, finally had to give up
I am a rookie, please do not attack, to be continued ....
PHP + Socket Programming Exercises--Blog Park reply