HTTP message structure
A client request consists of four parts: a request line (status line), a request header, a blank line, a request body (data), such as:
The service-side response consists of four parts: response Line (status line), response header, blank line, response body (data),
HTTP request Method:
POST, GET, Heade, PUT, TRACE, DELETE, OPTIONS, CONNECT (the top three most practical), there are so many request methods, but the Web server is not necessarily all supported.
GET is basically consistent, requesting the specified page information, and returning the entity body.
HEAD Basic is consistent with get, except that there is no specific content in the returned response to get the header
POST submits data to the specified resource for processing requests (such as forms, uploading files). The data is included in the request body. This request may result in the creation of new resources or modification of existing resources
Options returns the methods available to the server to allow the client to view the server's performance.
PUT replaces the contents of the specified document with the data that the client transmits to the server.
Delete Request server deletes the specified page
The Connect http/1.1 protocol is reserved for proxy servers that can change connections to pipelines.
TRACE echoes the requests received by the server, primarily for testing or diagnostics.
HTTP status Code
HTTP status Code five class
1xxx received request, continue processing
2XXX Request succeeded
3XXX redirection
4XXX Client Error
5xxx Server Request Error
Common normal Codes
200 Successful Request
301/302 Permanent/Temporary redirection
304 not modified, read from cache
404 resource does not exist
500 Server Internal Error
307 Redirect to keep the original data not lost
503 Server is temporarily unavailable
Based on the above on the HTTP has a certain understanding, according to the way the corresponding request for stitching
Php+socket Analog HTTP Requests
Interface
** *InterfacePhp_socket*/Interfacephp_socket{/** * @return Mixed*/ Public functionget (); /** * @return Mixed*/ Public functionPost$param); /** * @return Mixed*/ Public functionrequest (); /** * @return Mixed*/ Public functionHeader_info (); /** * @return Mixed*/ Public functionBody_info ($param); /** * @return Mixed*/ Public functionconnect ();}
Implementing interfaces
classImplement_socketImplementsphp_socket{protected $http _tpye= ' http/1.1 '; protected $url= ' '; protected $request _type= ' '; protected $lines= ' '; protected $fsoket _open= ' '; protected $port= ' '; protected $errstr= ' '; protected $timeout= 0; protected $parse _url= ' '; protected $content _type= ' '; protected $content _length= 0; protected $body= ' '; function__construct ($url,$request _type= ",$port= 80,$timeout= 5) { $this->url =$url; $this->request_type =$request _type; $this->port =$port; $this->timeout =$timeout; $this-Parse_url=Parse_url($url); //links $this-Connect (); } /** Set Request line **/ Public functionget () {$this->content_type = ' text/html '; $this->lines =$this->request_type. ‘ ‘ .$this-Parse_url[' Path ']. ‘ ‘ .$this-Http_tpye; $this-request (); } Public functionPost$param) { //Set Header information $this->content_type = ' application/x-www-form-urlencoded '; $data=$this->body_info ($param); $this->content_length =strlen($data); $this->lines =$this->request_type. ‘ ‘ .$this-Parse_url[' Path ']. ‘ ‘ .$this-Http_tpye; $this->body =$data; $this-request (); } Public functionrequest () {$getinfo= ' '; Echo $this->lines. "\ r \ n".implode("\ r \ n",$this->header_info ()). "\n\r\n".$this-body; Exit(); //link Successful Write header information fwrite($this->fsoket_open,$this->lines. "\ n".implode("\ n",$this->header_info ()). "\n\r\n".$this-body); while(!feof($this-Fsoket_open)) { $getinfo.=fgets($this->fsoket_open, 1024); } fclose($this-Fsoket_open); Echo"The following is the information obtained:<br>".$getinfo; } /** Link **/ Public functionConnect () {Try { $this->fsoket_open =Fsockopen($this-Parse_url[' Host '],$this->port,$this->ERRSTR,$this-timeout); } Catch(Exception $exception) { Echo' Connect is failed: '.$exception->getmessage (). ' r\n '.$this-Errstr; } } /** Set HEADER information **/ Public functionHeader_info () {return Array( "Host:".$this-Parse_url[' Host '], "Content-type:".$this->content_type, "Content-length:".$this-content_length); } /** Set Body **/ Public functionBody_info ($param) { //request string after generating Url-encode return Http_build_query($param); }}
Php+socket Analog HTTP Requests