简体   繁体   English

PHP带有响应的异步HTTP请求

[英]PHP Asynchronous HTTP request with response

I need to make about 10 requests in my script, and combine their responses. 我需要在脚本中提出约10个请求,并结合它们的响应。 However, if I do it by calling get_file_contents 10 times, it works much slower than for example calling $.json 10 times in Javascript. 但是,如果我通过调用get_file_contents 10次​​来执行此操作,则它的运行速度$.json如在Javascript中调用$.json 10 $.json慢得多。 I tried looking for ways to make asynchronous http requests in PHP, but every single piece of information I found started with "I don't need the response", which is not my case. 我试图寻找在PHP中发出异步http请求的方法,但是我发现的每条信息都以“我不需要响应”开头,这不是我的情况。

file_get_contents is a synchronous function, so when you call it 10 times, you call it 10 times sequentially. file_get_contents是一个同步函数,因此,当您调用10次时,将依次调用10次。 You need to make requests in parallel. 您需要并行发出请求。 curl_multi_* methods family is probably what you are looking for. curl_multi_ *方法家族可能是您想要的。 If you are not familiar with curl enough it may be tricky to implement it correctly, so I'd recommend you to use some library for that. 如果您对curl不够熟悉,那么正确地实现它可能会比较棘手,因此建议您使用一些库。

You can take a look at this library https://github.com/petewarden/ParallelCurl 您可以看一下这个库https://github.com/petewarden/ParallelCurl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM