Oara\Curl\Access::get PHP 메소드

get() 공개 메소드

Get
public get ( array $urls, integer $deep, $allowEmptyResult = false ) : array
$urls array
$deep integer
리턴 array
    public function get(array $urls, $deep = 0, $allowEmptyResult = false)
    {
        $results = array();
        $curlResults = array();
        $mcurl = \curl_multi_init();
        $threadsRunning = 0;
        $urls_id = 0;
        for (;;) {
            // Fill up the slots
            while ($threadsRunning < $this->_threads && $urls_id < \count($urls)) {
                $request = $urls[$urls_id];
                $ch = \curl_init();
                $chId = (int) $ch;
                $curlResults[(string) $chId] = '';
                $options = $this->_options;
                $options[CURLOPT_URL] = $request->getUrl() . self::getPostFields($request->getParameters());
                \curl_setopt_array($ch, $options);
                \curl_multi_add_handle($mcurl, $ch);
                $urls_id++;
                $threadsRunning++;
            }
            // Check if done
            if ($threadsRunning == 0 && $urls_id >= \count($urls)) {
                break;
            }
            // Let mcurl do it's thing
            \curl_multi_select($mcurl);
            while (($mcRes = \curl_multi_exec($mcurl, $mcActive)) == CURLM_CALL_MULTI_PERFORM) {
                \sleep(1);
            }
            if ($mcRes != CURLM_OK) {
                throw new \Exception('Fail in CURL access in GET, multiexec');
            }
            while ($done = \curl_multi_info_read($mcurl)) {
                $ch = $done['handle'];
                $chId = (int) $ch;
                $done_content = \curl_multi_getcontent($ch);
                if (!$allowEmptyResult && $done_content == false) {
                    if ($deep == 5) {
                        throw new \Exception('Fail in CURL access in GET, getcontent');
                    }
                    $keyPosition = self::keyPosition($curlResults, $chId);
                    $newUrlArray = array();
                    $newUrlArray[] = $urls[$keyPosition];
                    $newDeep = $deep + 1;
                    $recursion = self::get($newUrlArray, $newDeep);
                    $done_content = $recursion[0];
                }
                if (\curl_errno($ch) == 0) {
                    $curlResults[(string) $chId] = $done_content;
                } else {
                    throw new \Exception('Fail in CURL access in GET, getcontent');
                }
                \curl_multi_remove_handle($mcurl, $ch);
                \curl_close($ch);
                $threadsRunning--;
            }
        }
        \curl_multi_close($mcurl);
        foreach ($curlResults as $key => $value) {
            $results[] = $value;
        }
        return $results;
    }