Call multiple APIs simultaneously in PHP
P粉917406009
P粉917406009 2023-09-06 16:51:42
0
1
941

I'm trying to request multiple APIs as quickly as possible. So I triedcurl_multi. But I get slower results than foreach and file_get_contents. What did i do wrong?

Usingfile_get_contents:

Usingcurl_multi:

 0); for($i = 1; $i < $urls_count; $i++) { $results = curl_multi_getcontent ( $curl_arr[$i] ); } echo microtime(true) - $start; ?>

P粉917406009
P粉917406009

reply all (1)
P粉222320176

The problem is thatcurl_multihas a lot of overhead. I'm assuming it has to create a shell process for each request, then execute curl in that process, and finally return the content to the script that requested the action.file_get_contentsOptimized PHP language inherent:

This is a great learning experience for when to use libraries and native features in the language. Additionally, libraries can optionally be multi-threaded and take advantage of multi-core processors, which may speed up requests. Something to look up and test yourself.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!