PHP并发HTTP请求?
我想知道在 PHP 中执行并发 HTTP 请求的最佳方法是什么?我有很多数据要获取,我宁愿一次执行多个请求来检索所有数据。
有人知道我该怎么做吗?最好以匿名/回调函数的方式...
谢谢,
汤姆。
I was wondering what the best way to do concurrent HTTP requests in PHP? I have a lot of data to get and i'd rather do multiple requests at once to retrieve it all.
Does anybody know how I can do this? Preferably in an anonymous/callback function mannor...
Thanks,
Tom.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 curl_multi,它在单个curl下内部触发多个单独的请求处理。
但除此之外,PHP 本身不以任何方式/形状/形式“多线程”,并且不允许并行运行,除非通过严重的黑客攻击(多个并行脚本、一个脚本通过 exec() 启动多个后台任务等...... )。
You can use curl_multi, which internally fires off multiple separate requests under a single curl handle.
But otherwise PHP itself not in any way/shape/form "multithreaded" and will not allow things to run in parallel, except via gross hacks (multiple parallel scripts, one script firing up multiple background tasks via exec(), etc...).
您可以尝试 curl_multi() 或使用较低级别的函数 socket_select()
You can try either curl_multi() or use a lower level function socket_select()
您可以使用 HttpRequestPool http://www.php.net/manual/de/httprequestpool .construct.php
you can use HttpRequestPool http://www.php.net/manual/de/httprequestpool.construct.php
或者,如果您愿意,可以将数据作为 json 发送给您。在 php 中,您可以再次将其碎片整理为所有值。例如。
在 php 中,您可以按照以下步骤取回数据: 如何我如何使用 PHP 从 JSON 中提取数据?
因此,以 json 格式创建一个字符串,然后通过 http 发送整个内容。
我认为你可以使用 xml 执行相同的行为,但我不知道 xml
Or if you want you can send you data as json. In php you can defragment it into all the values again. for eg.
and in php you can follow this to get your data back: How do I extract data from JSON with PHP?
So make a string in json format and send the entire thing through http.
I think you can perform the same kind of behaviour with xml, but i am not aware of xml