As a PHP programmer must be exposed to the HTTP protocol, and only in-depth understanding of the HTTP protocol, programming level will be further. Recently I have been learning PHP for HTTP programming, a lot of things suddenly, benefited. I want to share it with you. This article requires developers with certain HTTP basics to read.
Today brings you how to use the socket to send a get,post request. I borrowed the Swallow 18 teacher encapsulated an HTTP class to be described.
In everyday programming I believe a lot of people are the same as me most of the time is to use the browser to make Get,post requests to the server, then can you use other methods to make Get,post requests? The answer must be yes. People who have known about the HTTP protocol know that the essence of a browser submission request is to send a request message to the server, which has a request line, a request header, and a request body (not required). The server returns a response message based on the requested information. The connection is broken.
The format of the HTTP request is as follows:
1
2
3
4 [
]
The format of the HTTP response is very similar to the format of the request:
[
]
We can use HTTP to send a request to the principle that you can reconsider the use of the socket to send HTTP requests.
The English literal of the socket is "hole" or "socket". Often also referred to as a "socket," which describes IP addresses and ports, is a handle to a communication chain that can be used to communicate between different virtual machines or different computers. Hosts on the internet typically run multiple service software, while providing several services. Each service opens a socket and binds to a port, and the different ports correspond to different services. In this case, it is just as easy to use the socket for remote files and read and write local files as the local files are transmitted through the hardware and the remote files are transmitted over the network cable.
Thus the sending request can be taken into account to establish connection, open socket Interface (Fsockopen ()), write Request (Fwrite ()), read-Out response (Fread (), Close file (fclose ()). Words not much to say, directly on the code:
Conn ($url); $this->setheader (' Host: ' $this->url[' host '); }//This method is responsible for writing the request line protected function Setline ($method) {$this->line[0] = $method. ' ' . $this->url[' path ']. '? '. $this->url[' query '. ' '. $this->version; }//This method is responsible for writing header information public function SetHeader ($headerline) {$this->header[] = $headerline; }//This method is responsible for writing the principal information protected function Setbody ($body) {$this->body[] = http_build_query ($body); }//Connection URL public Function conn ($url) {$this->url = Parse_url ($url); Determine the port if (!isset ($this->url[' Port ')) {$this->url[' port '] = 80; }//Judge query if (!isset ($this->url[' query ')) {$this->url[' query '] = '; } $this->fh = Fsockopen ($this->url[' host '), $this->url[' Port ', $this->errno, $this->errstr,3); }//construct data for GET request public function get () {$this->setline (' get '); $this->requesT (); return $this->response; }//Construct data Public function post for post query ($body = Array ()) {$this->setline (' post '); Design Content-type $this->setheader (' content-type:application/x-www-form-urlencoded '); Design the main information, the place is different than get $this->setbody ($body); Calculate content-length $this->setheader (' content-length: '. strlen ($this->body[0])); $this->request (); return $this->response; }//Really requests public Function request () {//To put the request line, header information, entity information in an array for easy splicing $req = Array_merge ($this->line, $this->header,array ("), $this->body,array (")); Print_r ($req); $req = Implode (Self::crlf, $req); Echo $req; Exit Fwrite ($this->fh, $req); while (!feof ($this->fh)) {$this->response. = fread ($this->fh,1024); } $this->close (); Close Connection}//close connection Public function close () {fclose ($this->FH); }}
Use this class to send a simple GET request:
Get ();p Rint_r ($response);
The return value is information that allows you to further process the response information to get what you want.