Oara\Curl\Access::post PHP Method

post() public method

Post
public post ( array $urls, integer $deep, $allowEmptyResult = false ) : array
$urls array
$deep integer
return array
    public function post(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();
                $options[CURLOPT_POST] = true;
                // Post form fields
                $arg = self::getPostFields($request->getParameters());
                $options[CURLOPT_POSTFIELDS] = $arg;
                \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 its 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 POST, 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 POST, getcontent');
                    }
                    $keyPosition = self::keyPosition($curlResults, $chId);
                    $newUrlArray = array();
                    $newUrlArray[] = $urls[$keyPosition];
                    $newDeep = $deep + 1;
                    $recursion = self::post($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 POST, getcontent');
                }
                \curl_multi_remove_handle($mcurl, $ch);
                \curl_close($ch);
                $threadsRunning--;
            }
        }
        \curl_multi_close($mcurl);
        foreach ($curlResults as $key => $value) {
            $results[] = $value;
        }
        return $results;
    }