Search This Blog

Monday, March 11, 2013

curl - Is making asynchronous HTTP requests possible with PHP? - Stack Overflow

curl - Is making asynchronous HTTP requests possible with PHP? - Stack Overflow:


Check out curl-easy. It supports both blocking and not-blocking requests in parallel or single request at once. Also, it is unit tested in opposite to many simple or buggy libraries.
Disclosure: I am the author of that library. Just saying. The library has it's own testsuite 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();

OTHER READS
http://petewarden.typepad.com/searchbrowser/2008/06/how-to-post-an.html
https://github.com/stil/curl-easy
http://us3.php.net/manual/en/book.libevent.php
http://w-shadow.com/blog/2007/10/16/how-to-run-a-php-script-in-the-background/

No comments:

Post a Comment