私は術前プログラマーであり、オブジェクトについてはあまり知りません。
私は次のクラスを持っています(SOで見つけました)
class MultiCurl
{
private $allToDo;
private $multiHandle;
private $maxConcurrent = 20;
private $currentIndex = 0;
private $info = array();
private $options = array(CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 1,
CURLOPT_TIMEOUT => 3);
public function __construct($todo, $concurrent)
{
$this->allToDo = $todo;
$this->maxConcurrent = $concurrent;
$this->multiHandle = curl_multi_init();
}
public function process()
{
$running = 0;
do {
$this->_addHandles(min(array($this->maxConcurrent - $running, $this->_moreToDo())));
while ($exec = curl_multi_exec($this->multiHandle, $running) === -1) {
}
curl_multi_select($this->multiHandle);
while ($multiInfo = curl_multi_info_read($this->multiHandle, $msgs)) {
$this->_showData($multiInfo);
curl_multi_remove_handle($this->multiHandle, $multiInfo['handle']);
curl_close($multiInfo['handle']);
}
} while ($running || $this->_moreTodo());
return $this;
}
private function _addHandles($num)
{
while ($num-- > 0) {
$handle = curl_init($this->allToDo[$this->currentIndex]);
curl_setopt_array($handle, $this->options);
curl_multi_add_handle($this->multiHandle, $handle);
$this->info[$handle]['url'] = $this->allToDo[$this->currentIndex];
$this->currentIndex++;
}
}
private function _moreToDo()
{
return count($this->allToDo) - $this->currentIndex;
}
private function _showData($multiInfo)
{
// $this->info[$multiInfo['handle']]['multi'] = $multiInfo;
$this->info[$multiInfo['handle']]['curl'] = curl_getinfo($multiInfo['handle']);
$this->info[$multiInfo['handle']]['content'] = curl_multi_getcontent($multiInfo['handle']);
return $this->info[$multiInfo['handle']];
}
}
私が実際にそれを使うとき:
$concurrent = 20;
$mc = new MultiCurl($nodes, $concurrent);
$output = $mc->process();
print_r($output);
すべての応答の多次元配列を出力するだけなので、「_showData」関数が返すものだけが返されます。
現在、次のような他の多くのものを返します。
[allToDo:MultiCurl:private] => Array
[multiHandle:MultiCurl:private] => Resource id #11
[maxConcurrent:MultiCurl:private] => 20
[currentIndex:MultiCurl:private] => 100
[info:MultiCurl:private] => Array
「[info:MultiCurl:private]」配列の内容のみが必要です