Is making asynchronous HTTP requests possible with PHP?
Solution 1:
Check out curl-easy. It supports both blocking and not-blocking requests in parallel or single request at once. Also, it is unit-tested, unlike many simple or buggy libraries.
Disclosure: I am the author of this library. The library has it's own test suite so I'm pretty confident it is robust.
Also, check out example of use below:
<?php
// We will download info about 2 YouTube videos:
// http://youtu.be/XmSdTa9kaiQ and
// http://youtu.be/6dC-sm5SWiU
// Init queue of requests
$queue = new cURL\RequestsQueue;
// Set default options for all requests in queue
$queue->getDefaultOptions()
->set(CURLOPT_TIMEOUT, 5)
->set(CURLOPT_RETURNTRANSFER, true);
// Set callback function to be executed when request will be completed
$queue->addListener('complete', function (cURL\Event $event) {
$response = $event->response;
$json = $response->getContent(); // Returns content of response
$feed = json_decode($json, true);
echo $feed['entry']['title']['$t'] . "\n";
});
$request = new cURL\Request('http://gdata.youtube.com/feeds/api/videos/XmSdTa9kaiQ?v=2&alt=json');
$queue->attach($request);
$request = new cURL\Request('http://gdata.youtube.com/feeds/api/videos/6dC-sm5SWiU?v=2&alt=json');
$queue->attach($request);
// Execute queue
$queue->send();
Solution 2:
Yes.
There is the multirequest PHP library (or see: archived Google Code project). It's a multi-threaded CURL library.
As another solution, you could write a script that does that in a language that supports threading, like Ruby or Python. Then, just call the script with PHP. Seems rather simple.
Solution 3:
For PHP5.5+, mpyw/co is the ultimate solution. It works as if it is tj/co in JavaScript.
Example
Assume that you want to download specified multiple GitHub users' avatars. The following steps are required for each user.
- Get content of http://github.com/mpyw (GET HTML)
- Find
<img class="avatar" src="...">
and request it (GET IMAGE)
---
: Waiting my response...
: Waiting other response in parallel flows
Many famous curl_multi
based scripts already provide us the following flows.
/-----------GET HTML\ /--GET IMAGE.........\
/ \/ \
[Start] GET HTML..............----------------GET IMAGE [Finish]
\ /\ /
\-----GET HTML....../ \-----GET IMAGE....../
However, this is not efficient enough. Do you want to reduce worthless waiting times ...
?
/-----------GET HTML--GET IMAGE\
/ \
[Start] GET HTML----------------GET IMAGE [Finish]
\ /
\-----GET HTML-----GET IMAGE.../
Yes, it's very easy with mpyw/co. For more details, visit the repository page.