是否可以使用 PHP 进行异步 HTTP 请求? [英] Is making asynchronous HTTP requests possible with PHP?

查看:41
本文介绍了是否可以使用 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.

这篇关于是否可以使用 PHP 进行异步 HTTP 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆