Beispiel #1
0
 /**
  * Retrieves the data using the config url. The default implementation uses the file_get_content()
  * function to retrieve the request. Subclasses would need to implement this if a simple GET request
  * is not sufficient (i.e. you need POST or custom headers). 
  * @return HTTPDataResponse a DataResponse object
  */
 protected function retrieveResponse()
 {
     $this->initRequestIfNeeded();
     if (!($this->requestURL = $this->url())) {
         throw new KurogoDataException("URL could not be determined");
     }
     $this->requestParameters = $this->parameters();
     if ($this->useCurl) {
         $this->responseHeaders = array();
         $this->requestMethod = $this->setCurlMethod();
         $this->requestHeaders = $this->setCurlHeaders();
         $this->requestData = $this->setCurlData();
     } else {
         $this->requestMethod = $this->setContextMethod();
         $this->requestHeaders = $this->setContextHeaders();
         $this->requestData = $this->setContextData();
     }
     Kurogo::log(LOG_INFO, "Retrieving {$this->requestURL}", 'url_retriever');
     $url_parts = parse_url($this->requestURL);
     if (!isset($url_parts['scheme'])) {
         $this->DEFAULT_RESPONSE_CLASS = "FileDataResponse";
     } else {
         $this->DEFAULT_RESPONSE_CLASS = "HTTPDataResponse";
     }
     $response = $this->initResponse();
     $response->setStartTime(microtime(true));
     if (!$this->showWarnings) {
         Kurogo::pushErrorReporting(E_ERROR);
     }
     if ($this->useCurl) {
         $this->setCurlOption(CURLOPT_URL, $this->requestURL);
         if ($file = $this->saveToFile()) {
             $data = $this->cache->getFullPath($file);
             $this->setCurlOption(CURLOPT_FILE, $data);
             $result = curl_exec($this->curl);
         } else {
             $data = curl_exec($this->curl);
             $response->setResponseError(curl_error($this->curl));
         }
     } else {
         if ($file = $this->saveToFile()) {
             $data = $this->cache->getFullPath($file);
             $result = file_put_contents($data, file_get_contents($this->requestURL, false, $this->streamContext));
         } else {
             $data = file_get_contents($this->requestURL, false, $this->streamContext);
         }
     }
     if (!$this->showWarnings) {
         Kurogo::popErrorReporting();
     }
     $response->setEndTime(microtime(true));
     if ($response instanceof HTTPDataResponse) {
         $response->setRequest($this->requestMethod, $this->requestURL, $this->requestParameters, $this->requestHeaders, $this->requestData);
         if ($this->useCurl) {
             $response->setResponseHeaders($this->responseHeaders);
         } else {
             $http_response_header = isset($http_response_header) ? $http_response_header : array();
             $response->setResponseHeaders($http_response_header);
         }
         Kurogo::log(LOG_DEBUG, sprintf("Returned status %d and %d bytes", $response->getCode(), strlen($data)), 'url_retriever');
     } elseif ($response instanceof FileDataResponse) {
         $response->setRequest($this->requestURL);
     }
     $response->setResponse($data);
     if ($response->getResponseError()) {
         Kurogo::log(LOG_WARNING, sprintf("%s for %s", $response->getResponseError(), $this->requestURL), 'url_retriever');
     }
     return $response;
 }