Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is making asynchronous HTTP requests possible with PHP?

Tags:

php

curl

I have a PHP script that needs to download several files from a remote server. At the moment I just have a loop downloading and processing the files with cURL, which means that it doesn't start downloading one file until the previous one is finished - this increases the script run time significantly.

Would it be possible to start several instances of cURL, for example, to asynchronously download these files at the same time without waiting for the previous one to finish? If so, how would this be accomplished?

like image 695
MarathonStudios Avatar asked Mar 27 '11 23:03

MarathonStudios


People also ask

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.

Does PHP support asynchronous?

PHP has no built in support for asynchronous calls. You can make pseudo-asynchronous calls using curl.

Which method is used to make an asynchronous HTTP request?

Ajax. Ajax is the traditional way to make an asynchronous HTTP request. Data can be sent using the HTTP POST method and received using the HTTP GET method.

Is PHP asynchronous by default?

When the recent PHP v/s Nodejs buzz came around, the one thing that always got the limelight was the fact that Nodejs implemented non-blocking (or asynchronous) I/O by default and PHP did not.


2 Answers

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(); 
like image 53
stil Avatar answered Sep 30 '22 18:09

stil


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.

like image 30
bl00dshooter Avatar answered Sep 30 '22 18:09

bl00dshooter