This article mainly introduces the PHP implementation of the HTTP request method, a simple analysis of the principle of HTTP request, flow and PHP implementation of the simulation HTTP request related operation skills, the need for friends can refer to the following
The example in this article describes how PHP implements a simulated HTTP request. Share to everyone for your reference, as follows:
In the HTTP brief, we mentioned a process of requesting resources from a browser, so can this process be simulated with PHP? The answer is yes.
The following steps are required for PHP to simulate HTTP requests:
1. Connect Apache Server
Use Fsockopen: Dedicated to connect to the server, get a connection resource
2. Writing to the HTTP protocol
Writing content to a resource using fwrite
3. Receiving data
Data returned after a successful request is stored in the resource
4. Parse the data:
Using Fgets, and FGETC functions
Implementation code:
The <?php //php Analog sends an HTTP request //1. Connect to the target server Apache $f =fsockopen (' localhost ', 98, $erron, $error); 2. Write HTTP protocol //2.1 Patchwork HTTP protocol //request line $http = "get/phpstudy/index.php http/1.1\r\n"; The request header $http. = "host:localhost\r\n"; Blank line $http. = "\ r \ n"; 2.2 to Apache server if (fwrite ($f, $http)) { //write succeeded //3. The data has been received and stored in the F resource //4. Parsing Resources // Loop through while ($line =fgets ($f, 1024x768)) { //output echo $line. </br> "; } }
Articles you may be interested in:
PHP implementation of socket push Technology example explained
How to get a shared delivery address in PHP
THINKPHP5 Project steps in cloud Virtualization host deployment