是否可以使用 PHP 进行异步 HTTP 请求?
我有一个 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.
是否可以启动多个 cURL 实例,例如同时异步下载这些文件,而无需等待前一个完成?如果是这样,这将如何实现?
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?
是的.
有 多请求 PHP 库(或参见:归档的 Google 代码项目).这是一个多线程的 CURL 库.
There is the multirequest PHP library (or see: archived Google Code project). It's a multi-threaded CURL library.
作为另一种解决方案,您可以编写一个脚本,以支持线程的语言(如 Ruby 或 Python)执行此操作.然后,只需使用 PHP 调用脚本即可.看起来很简单.
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.