1

我是一个前程序程序员,对对象不太了解。

我有以下课程(在 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]”数组的内容

4

4 回答 4

2

将公共方法添加到类“MultiCurl”。例如:

public function getInfo() {
  return $this->info;
}

如果您不想编辑类文件,请扩展并使用它。

Class MultiCurlWrapper extends MultiCurl {
    public function getInfo() {
      return $this->info;
    }
}
于 2012-04-24T22:28:29.277 回答
0

在处理方法中,而不是

return $this;

利用

return $this->info;
于 2012-04-24T22:28:34.900 回答
0

当当前函数返回对象时,我会添加一个方法来获取您需要的内容。

以最简单的形式;

public function getInfo()
{
  return $this->info;
}
于 2012-04-24T22:28:48.320 回答
0

print_r($output[info:MultiCurl:private]);工作吗?

如果$output数组的键总是相同的,你能用

print_r($output[4]);
于 2012-04-24T22:20:42.850 回答