Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 03:58
Show Gist options
  • Save code-boxx/b4c8e60c95c9f6ef7288deaa034127f0 to your computer and use it in GitHub Desktop.
Save code-boxx/b4c8e60c95c9f6ef7288deaa034127f0 to your computer and use it in GitHub Desktop.
PHP multi CURL request

MULTIPLE ASYNC CURL REQUESTS IN PHP

https://code-boxx.com/multi-async-curl-php/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?php
print_r($_POST);
<?php
// (A) INIT
// (A1) CURL TARGETS
$list = [
"http://localhost/dummy.php" => [
"post" => ["KeyA" => "ValueA", "KeyB" => "ValueB"],
"callback" => "output"
],
"https://en.wikipedia.org/wiki/Portal:Gardening" => [
"callback" => "output"
]
];
// (A2) CALLBACK FUNCTION - OUTPUT RESULTS
function output ($res) { echo $res; }
// (A3) PAUSE BETWEEN EACH CURL CALL (MS)
$pause = 1000;
// (B) MULTI CURL INIT
$mh = curl_multi_init();
$multi = [];
foreach ($list as $url=>$l) {
$i = count($multi);
$multi[$i] = curl_init();
curl_setopt($multi[$i], CURLOPT_URL, $url);
curl_setopt($multi[$i], CURLOPT_RETURNTRANSFER, 1);
if (isset($l["post"])) {
curl_setopt($multi[$i], CURLOPT_POST, true);
curl_setopt($multi[$i], CURLOPT_POSTFIELDS, $l["post"]);
}
curl_multi_add_handle($mh, $multi[$i]);
}
// (C) CURL EXEC
do {
// (C1) GET CURL EXEC STATUS
$status = curl_multi_exec($mh, $active);
// (C2) RUN CALLBACK WHEN CURL REQUEST IS COMPLETE
// CREDITS: https://gist.github.com/Xeoncross/2362936
if ($state = curl_multi_info_read($mh)) {
$info = curl_getinfo($state["handle"]);
if (isset($list[$info["url"]]["callback"])) {
$callback = $list[$info["url"]]["callback"];
$callback(curl_multi_getcontent($state["handle"]), $info);
}
curl_multi_remove_handle($mh, $state["handle"]);
}
// (C3) SHORT PAUSE TO NOT FLOOD SERVER
usleep($pause);
} while ($status == CURLM_CALL_MULTI_PERFORM || $active);
// (C4) CASE CLOSED - ALL DONE
curl_multi_close($mh);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment