request() public méthode

Options: 'URL' => NULL, 'Host' => NULL, // Override the Host: header 'Method' => 'GET', // HTTP Method 'ConnectTimeout' => 5, // Connection timeout 'Timeout' => 5, // Request timeout 'TransferMode' => 'normal', // or 'binary' 'SaveAs' => NULL, // Download the response to this file 'Redirects' => TRUE, // Allow 302 and 302 redirects 'SSLNoVerify' => FALSE, // Verify the remote SSL cert 'PreEncodePost' => TRUE, // 'Cookies' => TRUE, // Send user's browser cookies? 'CookieJar' => FALSE, // Create a cURL CookieJar? 'CookieSession' => FALSE, // Should old cookies be trashed starting now? 'CloseSession' => TRUE, // Whether to close the session. Should always do this. 'Redirected' => FALSE, // Is this a redirected request? 'Debug' => FALSE, // Debug output 'Simulate' => FALSE // Don't actually request, just set up
public request ( $Options = null, array $QueryParams = null, array $Files = null, array $ExtraHeaders = null ) : type
$QueryParams array GET/POST parameters
$Files array List of files to upload
$ExtraHeaders array Any additional headers to tack on
Résultat type
 /**
  *
  *
  * @param $Method
  * @param $RequestParameters
  * @param bool $Callback
  * @param bool $ParseResponse
  * @return array|bool|mixed|type
  * @throws Exception
  */
 public function analytics($Method, $RequestParameters, $Callback = false, $ParseResponse = true)
 {
     $FullMethod = explode('/', $Method);
     if (sizeof($FullMethod) < 2) {
         array_unshift($FullMethod, "analytics");
     }
     list($ApiController, $ApiMethod) = $FullMethod;
     $ApiController = strtolower($ApiController);
     $ApiMethod = stringEndsWith(strtolower($ApiMethod), '.json', true, true) . '.json';
     $FinalURL = 'http://' . combinePaths(array($this->AnalyticsServer, $ApiController, $ApiMethod));
     $RequestHeaders = array();
     // Allow hooking of analytics events
     $this->EventArguments['AnalyticsMethod'] =& $Method;
     $this->EventArguments['AnalyticsArgs'] =& $RequestParameters;
     $this->EventArguments['AnalyticsUrl'] =& $FinalURL;
     $this->EventArguments['AnalyticsHeaders'] =& $RequestHeaders;
     $this->fireEvent('SendAnalytics');
     // Sign request
     $this->sign($RequestParameters, true);
     $RequestMethod = val('RequestMethod', $RequestParameters, 'GET');
     unset($RequestParameters['RequestMethod']);
     try {
         $ProxyRequest = new ProxyRequest(false, array('Method' => $RequestMethod, 'Timeout' => 10, 'Cookies' => false));
         $Response = $ProxyRequest->request(array('Url' => $FinalURL, 'Log' => false), $RequestParameters, null, $RequestHeaders);
     } catch (Exception $e) {
         $Response = false;
     }
     if ($Response !== false) {
         $JsonResponse = json_decode($Response, true);
         if ($JsonResponse !== false) {
             if ($ParseResponse) {
                 $AnalyticsJsonResponse = (array) val('Analytics', $JsonResponse, false);
                 // If we received a reply, parse it
                 if ($AnalyticsJsonResponse !== false) {
                     $this->parseAnalyticsResponse($AnalyticsJsonResponse, $Response, $Callback);
                     return $AnalyticsJsonResponse;
                 }
             } else {
                 return $JsonResponse;
             }
         }
         return $Response;
     }
     return false;
 }
Exemple #2
0
 /**
  * Generic API uses ProxyRequest class to fetch data from remote endpoints.
  *
  * @param $uri Endpoint on provider's server.
  * @param string $method HTTP method required by provider.
  * @param array $params Query string.
  * @param array $options Configuration options for the request (e.g. Content-Type).
  *
  * @return mixed|type.
  *
  * @throws Exception.
  * @throws Gdn_UserException.
  */
 protected function api($uri, $method = 'GET', $params = [], $options = [])
 {
     $proxy = new ProxyRequest();
     // Create default values of options to be passed to ProxyRequest.
     $defaultOptions['ConnectTimeout'] = 10;
     $defaultOptions['Timeout'] = 10;
     $headers = [];
     // Optionally over-write the content type
     if ($contentType = val('Content-Type', $options, $this->defaultContentType)) {
         $headers['Content-Type'] = $contentType;
     }
     // Obtionally add proprietary required Authorization headers
     if ($headerAuthorization = val('Authorization-Header-Message', $options, null)) {
         $headers['Authorization'] = $headerAuthorization;
     }
     // Merge the default options with the passed options over-writing default options with passed options.
     $proxyOptions = array_merge($defaultOptions, $options);
     $proxyOptions['URL'] = $uri;
     $proxyOptions['Method'] = $method;
     $this->log('Proxy Request Sent in API', ['headers' => $headers, 'proxyOptions' => $proxyOptions, 'params' => $params]);
     $response = $proxy->request($proxyOptions, $params, null, $headers);
     // Extract response only if it arrives as JSON
     if (stripos($proxy->ContentType, 'application/json') !== false) {
         $this->log('API JSON Response', ['response' => $response]);
         $response = json_decode($proxy->ResponseBody, true);
     }
     // Return any errors
     if (!$proxy->responseClass('2xx')) {
         if (isset($response['error'])) {
             $message = 'Request server says: ' . $response['error_description'] . ' (code: ' . $response['error'] . ')';
         } else {
             $message = 'HTTP Error communicating Code: ' . $proxy->ResponseStatus;
         }
         $this->log('API Response Error Thrown', ['response' => json_decode($response)]);
         throw new Gdn_UserException($message, $proxy->ResponseStatus);
     }
     return $response;
 }