Php uses socket to send GET, POST, and socketget requests. Php uses socket to send GET and POST requests. as a php programmer, socketget will certainly be exposed to the http protocol, and the programming level will go further only after a deep understanding of the http protocol. Recently I have been using php to send GET, POST requests, 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
2
3
4 [
]
The HTTP response format is very similar to the request format:
[
]
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:
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:
// 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.
Http://www.bkjia.com/PHPjc/1040167.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1040167.htmlTechArticlephp uses socket to send GET, POST request, socketget as a php programmer will be exposed to the http protocol, and only a deep understanding of the http protocol, programming level will go further. Recently I have been in...