Last active
August 29, 2015 14:01
-
-
Save sndsgd/3883c8af9aa2cd1669dc to your computer and use it in GitHub Desktop.
Perform multiple cURL requests in parallel with PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * perform multiple curl requests in parallel | |
| * @param array.<string> $urls - an indexed array of urls to fetch | |
| * @param array.<integer> $curopts - curl options to pass to curl_setopt_array() | |
| * @return array | |
| */ | |
| function curlMulti(array $urls, array $curlopts) | |
| { | |
| $ret = []; | |
| $chs = []; | |
| $mh = curl_multi_init(); | |
| $len = count($urls); | |
| for ($i=0; $i<$len; $i++) { | |
| $chs[$i] = curl_init($urls[$i]); | |
| curl_setopt_array($chs[$i], $curlopts); | |
| curl_multi_add_handle($mh, $chs[$i]); | |
| } | |
| do { | |
| curl_multi_exec($mh, $active); | |
| curl_multi_select($mh, .1); | |
| } | |
| while ($active); | |
| for ($i=0; $i<$len; $i++) { | |
| $info = curl_getinfo($chs[$i]); | |
| $ret[$info['url']] = [ | |
| 'info' => $info, | |
| 'content' => mb_convert_encoding( | |
| curl_multi_getcontent($chs[$i]), | |
| 'HTML-ENTITIES', | |
| 'UTF-8' | |
| ) | |
| ]; | |
| } | |
| curl_multi_close($mh); | |
| return $ret; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment