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; ?>
The problem is that
curl_multi
has 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_contents
Optimized 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.