Php uses socket to send GET, POST request, socketget
As a php programmer, he must be familiar with the http protocol. He must have an in-depth understanding of the http protocol and the programming level will be further improved. Recently I have been learning about http programming in php. Many things have suddenly become enlightened and benefited a lot. I hope to share it with you. This article needs to be read by developers who have certain http basics.
What we bring to you today is how to use socket to send GET and POST requests. I used an Http class encapsulated by instructor Yan 18 to explain it.
In daily programming, I believe that many people, like me, spend most of their time using browsers to send GET and POST requests to the server. Can they use other methods to submit GET and POST requests? The answer must be yes. People who know the HTTP protocol know that the essence of a browser request is to send a request information to the server, which consists of the request line, request header, and request body (not required. The server returns a response message based on the request information. The connection is disconnected.
The HTTP request format is as follows:
1 <request-line>2
The HTTP Response format is very similar to the request format:
<status-line>
We can use the principle of HTTP to send requests, and re-consider using socket to send HTTP requests.
The original meaning of Socket is "hole" or "Socket ". Generally referred to as "socket", which is used to describe IP addresses and ports. It is a communication chain handle and can be used to implement communication between different virtual machines or different computers. Hosts on the Internet generally run multiple service software and provide several services at the same time. Each service opens a Socket and binds it to a port. Different ports correspond to different services. In this case, using socket to operate remote files is as easy as reading and writing local files. You just need to use the network cable to transmit local files through hardware.
Therefore, you can consider sending requestsEstablish a connection-> open socket interface (fsockopen ()-> Write Request (fwrite ()-> Read response (fread ()-> close file (fclose ()). If you don't talk much about it, go directly to the Code:
<? Php interface Proto {// connect 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 writes the request line protected function setLine ($ method) {$ this-> line [0] = $ method. ''. $ this-> url ['path']. '? '. $ This-> url ['query']. ''. $ this-> version;} // this method writes the header information public function setHeader ($ headerline) {$ this-> header [] = $ headerline ;} // this method writes the subject information protected function setBody ($ body) {$ this-> body [] = http_build_query ($ body );} // connect 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 public function get () {$ this-> setLine ('get') of the get request '); $ this-> request (); return $ this-> response;} // construct the post query data public function post ($ body = array ()) {$ this-> setLine ('post'); // Design content-type $ this-> setHeader ('content-type: application/x-www -Form-urlencoded '); // design the subject information, which is different from GET. $ this-> setBody ($ body ); // calculate content-length $ this-> setHeader ('content-length :'. strlen ($ this-> body [0]); $ this-> request (); return $ this-> response ;}// the public function request () is actually requested () {// put the request line, header information, and object information in an array to facilitate splicing $ req = array_merge ($ this-> line, $ this-> header, array (''), $ this-> body, array (''); // print_r ($ req); $ req = implode (self: CRLF, $ req); // echo $ req; e Xit; 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 = "http://home.cnblogs.com/u/DeanChopper/"; $ http = new Http ($ url); $ response = $ http-> get (); print_r ($ response );
The returned value is information. You can further process the response information to obtain the desired content.