For more information about the fsockopen function in PHP, see how the manual defines the fsockopen function.
Fsockopen-open a network connection or a Unix socket connection.
ResourceFsockopen(String $ hostname [, int $ port =-1 [, int & $ errno [, string & $ errstr [, float $ timeout = ini_get ("default_socket_timeout")]
Both the fsockopen method and the CURL method simulate the HTTP protocol, so the focus is to construct the HTTP protocol.
Note: To use this function, open allow_url_open in the PHP. ini file.
// Open the connection channel $ fp = fsockopen ('www .cnblogs.com ', 80, $ errno, $ errstr, 30); if (! $ Fp) {die ('connection error '. $ errstr);} set_time_limit (0); // The channel is opened to simulate HTTP requests, the standard line feed of http is \ r \ n $ http = "GET/HTTP/1.1 \ r \ n"; $ http. = "Host: www.cnblogs.com \ r \ n"; $ http. = "User-Agent: $ _ SERVER [HTTP_USER_AGENT] \ r \ n"; $ http. = "Connection: Close \ r \ n"; // The http simulation is completed and sent to the server fwrite ($ fp, $ http ); // accept the server's returned result $ out = ''; while (! Feof ($ fp) {$ out. = fread ($ fp, 1024);} echo $ out; fclose ($ fp );
This problem is often encountered during the development process. you can simulate a browser to access an interface and obtain the returned data. We usually use fsockopen to establish a connection with the interface, then issue the command, and then accept the return value through fgets.
However, we found that simulating an access interface through PHP is often much slower than accessing the same interface through a browser. This problem has plagued me for a long time and I finally found the cause today. I think many of my friends on the Internet share the same question for your reference.
We often write such code:
| 1 |
While (! Feof ($ sHnd )){ |
| 2 |
$ Line = fgets ($ sHnd, 4096 ); |
Fgets will get the current 4096 (or another constant) bytes of the file descriptor $ sHnd. if there is no line break between 4096 bytes, only the content before the line break is returned.
This is also true in many document and tutorial documents. this code can be executed normally in many cases. I tracked the time consumed by PHP statements step by step and found that the previous fgets were fast. the most time consumed was the last fgets, sometimes several seconds or even dozens of seconds.
Originally, this was caused by the KeepAlive function of the server. Apache has such a setting (nginx and other web servers also have it): KeepAlive. if this setting is set to On, after a request is completed, the server does not close the TCP connection, but keeps the connection waiting for the browser to directly use this connection when initiating other requests. However, when fgets obtains the last part of the content, it does not find a line break or the end mark (feof (). Therefore, fgets continues to wait after obtaining the content, expected line breaks or other content to exceed 4096 characters. As a result, the servers and PHP are consuming and waiting for each other. After a while, the server will not be able to afford it. after all, the number of connections on the server is very valuable. when the connection is not active for several seconds, the connection will be closed (Apache uses the KeepAliveTimeout option to set the connection, this value is usually 5-15 ). After the server closes the connection, fgets on the PHP side is lost and the last batch of content is returned. the access interface process ends.
After knowing the cause of the slowness, we can see the solution:
The HTTP header returned by the server contains the content length attribute. when the accepted content length is the same, we can conclude that the interface content has been obtained and you don't have to wait. The specific method is: get content that does not exceed the total length (min (4096, $ leftlength) each time )). When the total length is 0, the loop of while (feof ($ xxxxx) exists.
After such modification, php's slow interface access speed through sock has been basically solved, but it is still not perfect. the idea of speed optimization is still on KeepAlive.
As we all know, a considerable amount of access interface time is wasted on establishing connections. if we need to call interfaces frequently, there is still much room for optimization. Since the server maintains a connection, if PHP also saves the connection, isn't it necessary to establish a connection? The answer is yes: when accessing the interface for the first time, pfsockopen (the only difference between pfsockopen and fsockopen is that it establishes a persistent connection) function to establish a connection with the server, after the access is complete, the (fclose) connection is not closed, and will be directly used for future access. Specifically in the code: first determine whether there is a connection, if there is, continue to use, if not, establish a pfsockopen connection.
In addition, if the content returned by the interface is relatively short (for example, less than 50 characters), there is room for optimization, that is, remove gzip from the Accept-Encoding of the HTTP request header. It is used to tell the server that I (the browser) can accept compressed content. if the server also supports gzip, it will compress the content and then return it. after the browser obtains the content, decompress it and then display it. However, if the content is too short, the volume will increase after compression, plus the compression and decompression time, it will be even worse.
After the above steps, the access interface speed should be the same as that of the browser, and theoretically it will be a little faster.