Php non blocking http request
Event ExtensionEvent extension is very appropriate. It is a port of Libevent library which is designed for event-driven I/O, mainly for networking. Show I have written a sample HTTP client that allows to schedule a number of HTTP requests and run them asynchronously. This is a sample HTTP client class based on Event extension. The class allows to schedule a number of HTTP requests, then run them asynchronously. http-client.php
test.phpThis is a sample script on the server side.
Usage
Sample Output
(Trimmed.) Note, the code is designed for long-term processing in the CLI SAPI. For custom protocols, consider using low-level API, i.e. buffer events, buffers. For SSL/TLS communications, I would recommend the low-level API in conjunction with Event's ssl context. Examples:
Although Libevent's HTTP API is simple, it is not as flexible as buffer events. For example, the HTTP API currently doesn't support custom HTTP methods. But it is possible to implement virtually any protocol using the low-level API. Ev ExtensionI have also written a sample of another HTTP client using Ev extension with sockets in non-blocking mode. The code is slightly more verbose than the sample based on Event, because Ev is a
general purpose event loop. It doesn't provide network-specific functions, but its This is a sample HTTP client based on Ev extension. Ev extension implements a simple yet powerful general purpose event loop. It doesn't provide network-specific watchers, but its I/O watcher can be used for asynchronous processing of sockets. The following code shows how HTTP requests can be scheduled for parallel processing. http-client.php
TestingSuppose
Then the output
of
(trimmed) Note, in PHP 5 the sockets extension may log warnings for
Concerning "the Rest" of the Code
The code that is supposed to run in parallel with the network requests can be executed within a the callback of an Event timer, or Ev's idle watcher, for instance. You can easily figure it out by watching the samples mentioned above. Otherwise, I'll add another example :) Can PHP send asynchronous request?Guzzle 6: Guzzle is a PHP HTTP client helps to send the HTTP requests. These methods can be used to send the asynchronous HTTP requests.
Can PHP run async?Yes. Because asynchronous processing enables the management and completion of more than one task at a time, you can dramatically boost PHP performance by using it instead of synchronous PHP.
Is PHP Curl asynchronous?Short answer is no it isn't asynchronous. Longer answer is "Not unless you wrote the backend yourself to do so." If you're using XHR, each request is going to have a different worker thread on the backend which means no request should block any other, barring hitting process and memory limits.
How do I run a PHP script asynchronously?php /* * Executes a PHP page asynchronously so the current page does not have to wait for it to finish running. * */ function post_async($url, array $params) { foreach ($params as $key => &$val) { if (is_array($val)) $val = implode(',', $val); $post_params[] = $key. '='.
|