开发者

what is the best practice to do multiple curl requests with different parameters?

开发者 https://www.devze.com 2023-03-21 02:56 出处:网络
I have a simple PHP program that list some topics. for example I have an array [1] => Red [2] => Green [3] => Blue

I have a simple PHP program that list some topics.

for example I have an array [1] => Red [2] => Green [3] => Blue

and then I want to get pictures of those three using, say Google API

So I will do this

foreach ($array as $arr)
{
     echo curl_get_image($arr);
}

So my problem here is that I'm not only have 3 items in the array, it can be 10 or 20 The program run really slowly, I suspect that it is because 开发者_运维问答the program has to wait for each curl request before going to the next curl request.

what is the best way to do this?

or is there any better way like multithreading the curl processes to run each process in parallel?

Thank You


The best way to do something like this is with curl_multi_init. There's a simple example provided in the documentation that sends 2 cURL requests at the same time. You could easily implement something that does as many requests in parallel as you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消