As a php programmer, you will definitely be exposed to the http protocol. Only by deeply understanding the http protocol can your programming level go further. Recently, I have been learning about http programming in PHP, and I suddenly realized many things and benefited a lot. Hope to share it with everyone. This article needs to be read by developers with a certain HTTP foundation.
What I bring to you today is how to use socket to send GET and POST requests. I borrowed an Http class encapsulated by Teacher Yan Shiba for explanation.
In daily programming, I believe that many people, like me, spend most of their time using the browser to make GET and POST requests to the server. So can they use other methods to make GET and POST requests? The answer must be yes. Anyone who has understood the HTTP protocol knows that the essence of a browser submitting a request is to send a request information to the server, which consists of a request line, a request header, and a request body (not necessary). The server returns a response information based on the request information. Disconnected.
The format of HTTP request is as follows:
<request-line> <headers> <blank line> [<request-body>]
The format of the HTTP response is very similar to the format of the request:
<status-line> <headers> <blank line> [<response-body>]
We can use the principle of sending HTTP requests and can reconsider sending HTTP requests using sockets.
The original meaning of Socket is "hole" or "socket". Commonly referred to as "sockets", used to describe IP addresses and ports, is a handle to a communication chain that can be used to implement communication between different virtual machines or different computers. The host on the Internet generally runs multiple service software and provides several services at the same time. Each service opens a Socket and binds to a port, and different ports correspond to different services. In this way, using sockets to operate remote files is as easy as reading and writing local files. Just treat local files as hardware transmission, and remote files are transmitted over a network cable.
Therefore, the sending request can be considered as establishing a connection -> opening socket interface (fsocopen()) -> writing request (fwrite()) -> reading response (fread()-> closing file (fclose()). Without further ado, just go to the code:
<?php interface Proto { // Connect to url function conn($url); //Send get query function get(); // Send post query function post(); // Close the connection function close(); } class Http implements Proto { const CRLF = "\r\n"; protected $errno = -1; protected $errstr = ''; protected $response = ''; protected $url = null; protected $version = 'HTTP/1.1'; protected $fh = null; 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 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 subject information protected function setBody($body) { $this->body[] = http_build_query($body); } // Connect to url public function conn($url) { $this->url = parse_url($url); //Judge 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 the data of the get request public function get() { $this->setLine('GET'); $this->request(); return $this->response; } // Construct the data for post query public function post($body = array()) { $this->setLine('POST'); // Design content-type $this->setHeader('Content-type: application/x-www-form-urlencoded'); // Design subject information is different from GET $this->setBody($body); // Calculate content-length $this->setHeader('Content-length: ' . strlen($this->body[0])); $this->request(); return $this->response; } // Real request public function request() { // Put request lines, header information, and 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 the connection } // Close the connection public function close() { fclose($this->fh); } }
Use this class to send a simple GET request:
<?php//Remember to reference the Http class$url="/u/DeanChopper/"; $http=new Http($url); $response=$http->get(); print_r($response);
The return value is information, and the response information can be further processed to obtain the content you want.
The above is the detailed content of the instance code of php using socket to send GET and POST requests. For more information about php sending GET and POST requests, please pay attention to my other related articles!