First of all, this article uses a previous message board case to test, case connection: Use post to realize the message board message. The previous section is an explanation of the principle of the steps, and the second half is the code implementation that continues with the previous article. So here we go:
1. The message page is as follows:
First submit a message, then grab the packet to view the post data.
See this sentence:
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:14PX;" >title=vEagleFly&author=vEagleFly&content=vEagleFly</span>
This is the data we submitted.
So, we're going to start imitating the implementation. In fact, we have encountered here before, is this article, HTTP application of simulated irrigation robot, the truth is the same. It's just that we change the way that the class implements it.
Check the message to achieve success.
"Code Implementation":
<span style= "font-family:kaiti_gb2312;" ><?
PHP/* PHP + Socket Programming HTTP request * * *//HTTP Request Class Interface interface proto{//connection URL function conn ($url);
Send a Get Query function get ();
Send post query function post ();
Closes the connection function close ();
Class Httpimplements proto{//define a carriage return line, under Linux is ' \ n ' under Windows ' \ r \ n '//http standard is ' \ r \ n ' const CRLF = ' \ r \ n ';
protected $errno =-1;
protected $errstr = ';
protected $response = ';
protected $url = null;
protected $fh = null;
protected $version = ' http/1.1 ';
protected $line = Array ();
Protected $header = Array ();
Protected $body = Array ();
Public function __construct ($url) {$this-> conn ($url);
$this-> SetHeader (' Host: ' $this->url[' host ')); }//This method is responsible for write request line protected function Setline ($method) {$this-> line[0] = $method. $this->url[' path ']. '
'. $this->version; }//This method is responsible for writing header information protected function SetHeader ($headerline) {$this-> header[] = $Headerline;
}//This method is responsible for writing the body information protected function Setbody ($body) {$this-> body[] =http_build_query ($body);
}//Connection URL public Function conn ($url) {$this-> url = parse_url ($url);
To determine the port if (!isset ($this->url[' Port ')) {$this-> url[' port ' = 80; $this-> fh = fsockopen ($this->url[' host '), $this-> url[' Port ', $this-> errno, $this-> errstr,3)
;
}//Construct GET Request data public function get () {$this-> setline (' get ');
$this-> request ();
}//construct POST request data public Function post ($body = Array ()) {$this-> setline (' post ');
$this-> setbody ($body);
$this-> SetHeader (' content-length: ' strlen ($this->body[0));
$this-> setheader (' content-type:application/x-www-form-urlencoded ');
$this-> request ();
//Send Get requests Public Function request () {//Put the request line, header information, entity information in an array for easy stitching. $req = Array_merge ($this-> line, $this->Header,array ("), $this-> Body,array ("));
$req = Implode (Self::crlf, $req);
Echo $req;
Fwrite ($this->fh, $req);
while (!feof ($this->fh)) {$this-> response. =fread ($this->fh,1024);
echo $this-> response;
$this-> Close ();//Turn off Connection}//Turn off connection public function close () {$fclose ($this-> FH);
}/* $url = ' http://localhost/test.php ';
$http = Newhttp ($url);
$http-> get ();
* * $url = ' http://127.0.0.1/liuyan/doAdd.php ';
$post = Newhttp ($url);
$post->post (' title ' => ' Ceshi ', ' author ' => ' veaglefly ', ' content ' => ' no ')); </span>
"Run Results":
At this point, using socket programming to implement get and post send request data has been fully implemented.
"Knowledge development":
To achieve bulk posting (ie irrigation), add a For loop.
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:14PX;" >set_time_limit (0);
$url = ' http://127.0.0.1/liuyan/doAdd.php ';
for ($i = 0; $i <10; $i + +) {
$post = new Http ($url);
$post->post (' title ' => ' Guanshui ', ' author ' => ' veaglefly ', ' content ' => ' haha '));
Usleep (20000)//delay 2s send, too fast own machine can not afford.
}</span>