Пример #1
0
 /**
  * The method which both loadSingle and loadSingleStatic calls, and which is the "real" loading-function
  * @param resource $curlHandler
  * @param SplFileObject|null $historyFileObject The instance method loadSingle may pass its existing fileObject
  * @param string $url Url to load, obviously
  * @param array|null $formdata If null request is sent as GET, otherwise POST and with the postdata of this argument
  * @param array $settings See parameter $settings of loadSingle()
  * @param array $history See parameter $history of loadSingle()
  * @return array ['content' string,'headers' array,'error' string|null,'errorCode']*/
 private static function loadSingleReal($curlHandler, $historyFileObject, $url, $formdata, $settings, $history = null)
 {
     $numRetries = -1;
     Hicurl::setCurlOptions($curlHandler, $url, $formdata, $settings);
     $output = [];
     //this is the array that will be returned
     if ($historyFileObject || !empty($settings['history'])) {
         //should we write history?
         //see description of writeHistory() for explanation of the history-structure
         $historyPage = ['formData' => $formdata, 'exchanges' => []];
         if ($history) {
             //add customData and name of the $history-parameter to $historyPage
             $historyPage += array_intersect_key($history, array_flip(['customData', 'name']));
         }
     }
     do {
         if (++$numRetries) {
             if ($numRetries == $settings['maxFruitlessRetries']) {
                 $content = $headers = null;
                 $output['error'] = $error;
                 break;
             }
             sleep($settings['fruitlessPassDelay']);
         }
         $content = curl_exec($curlHandler);
         //do the actual request. assign response-content to $content
         $headers = curl_getinfo($curlHandler);
         //get the headers too
         if ($headers['http_code'] == 0 && !empty($settings['tor'])) {
             $output['errorCode'] = 1;
             $output['error'] = "Unable to connect via tor-proxy.\nIs Tor installed and configured correctly?";
             $content = $headers = null;
             break;
         }
         $error = Hicurl::parseAndValidateResult($content, $headers, $settings, $output);
         if (isset($historyPage)) {
             //are we writing history-data? this var is only set if we are
             $historyPage['exchanges'][] = ['content' => $content, 'headers' => $headers, 'error' => $error, 'errorCode' => 0];
         }
     } while ($error);
     //keep looping until $error is false
     if (isset($historyPage)) {
         //should we write history?
         Hicurl::writeHistory($historyFileObject, $historyPage, $settings, $history);
     }
     return $output += ['content' => $content, 'headers' => $headers, 'error' => false];
 }