dev-master
9999999-dev
The Requires
- php >=5.3.0
The Development Requires
0.1.0
0.1.0.0
The Requires
- php >=5.3.0
The Development Requires
Wallogit.com
2017 © Pedro Peláez
This iterator class makes it simple to use PHP's multicurl effectively., (*1)
However, it does not curl_init() for you. Instead, it gives you full control over what requests you'll be making., (*2)
Composer/Packagist - https://packagist.org/packages/alexpw/multicurl-iterator, (*3)
$mci = new Alexpw\Multicurl\Iterator();
foreach ($curl_handles as $handle) {
$mci->add($handle /*[, mixed $data = null] */);
}
foreach ($mci as $result) {
doSomething($result);
}
It executes as many curl requests in parallel as you allow it. As soon as the first response is received, the foreach is allowed to continue, and you are given the $result of parsing the response., (*4)
An approximation:, (*5)
$result = curl_getinfo($ch); $result['handle'] = $ch; $result['data'] = // Optional data associated with curl handle $result['header'] = $header_string_or_parsed_array; $result['body'] = $body_string; $result['errno'] = curl_errno($ch); $result['error'] = curl_error($ch); $result['errorstr'] = curl_errstr($ch); // when function_exists
The results are returned out of order and as soon as they are ready, but you can ID them easily., (*6)
The most convenient:
$mci->add($handle, $data = null);, (*7)
$data can be anything that's meaningful to you:, (*8)
$mci->setMaxExecuting(10); // default: 10, (*9)
Parse the response header as an array (if your request asked for headers).
$mci->setParseResponseHeader(true); // default: true, (*10)
Whether to automatically close curl handles after using them., (*11)
$mci->setCloseCurlHandles(true); // default: true, (*12)
Note, if you want to be able to reuse curl handles for retries or whatever, you'll need to manage and close them yourself., (*13)
You don't need to create all of your curl handles in advance, they can be added at any time., (*14)
$article_ids = array(1, 2, 3, 4 /*,...*/);
$article_id_chunks = array_chunk($article_ids, 50);
$curr_chunk = 0;
$total_chunks = count($article_id_chunks);
function addArticlesToIterator($mci, $chunk)
{
foreach ($chunk as $article_id) {
$ch = initCurlHandleForArticle($article_id);
$mci->add($ch, $article_id);
}
}
$mci = new Alexpw\Multicurl\Iterator();
$mci->setMaxExecuting(6);
addArticlesToIterator($mci, $article_id_chunks[$curr_chunk++]);
foreach ($mci as $result) {
if ($mci->getCountPendingRequests() < 10 &&
$curr_chunk < $total_chunks) {
addArticleRequests($mci, $article_id_chunks[$curr_chunk++]);
}
if ($result['errno'] !== 0) {
logFailed($result);
} elseif ($result['http_code'] === 408) {
$ch = initCurlHandleForRetry($result);
$mci->add($ch);
} else {
doSomething($result);
}
}
Multicurl Iterator is licensed under the MIT License - see the LICENSE file for details., (*15)