From aebaef528b39f1a8b647c0ea0aebf4cf22b4d8f3 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Fri, 23 Sep 2022 07:06:39 -0700 Subject: [PATCH] Add example for queuing requests dynamically --- .../multi_curl_add_curl_from_url_list.php | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 examples/multi_curl_add_curl_from_url_list.php diff --git a/examples/multi_curl_add_curl_from_url_list.php b/examples/multi_curl_add_curl_from_url_list.php new file mode 100644 index 0000000..24eebfc --- /dev/null +++ b/examples/multi_curl_add_curl_from_url_list.php @@ -0,0 +1,41 @@ +complete(function ($instance) use (&$multi_curl, &$urls) { + echo 'complete:' . $instance->url . "\n"; + + // Queue another request each time a request completes. Fetch the oldest url + // next using array_shift($urls) or use the most recently added url using + // array_pop($urls). + // $next_url = array_shift($urls); + // $next_url = array_pop($urls); + $next_url = array_shift($urls); + + if ($next_url !== null) { + $multi_curl->addGet($next_url); + } +}); + +// Queue a few requests. +$concurrency = 3; +for ($i = 0; $i < $concurrency; $i++) { + $next_url = array_shift($urls); + if ($next_url !== null) { + $multi_curl->addGet($next_url); + } +} + +$multi_curl->start();