コード例 #1
0
 /**
  * Check if an HTTP request can be cached by a private local cache.
  *
  * @static
  * @param Google_Http_Request $resp
  * @return bool True if the request is cacheable.
  * False if the request is uncacheable.
  */
 public static function isRequestCacheable(Google_Http_Request $resp)
 {
     $method = $resp->getRequestMethod();
     if (!in_array($method, self::$CACHEABLE_HTTP_METHODS)) {
         return false;
     }
     // Don't cache authorized requests/responses.
     // [rfc2616-14.8] When a shared cache receives a request containing an
     // Authorization field, it MUST NOT return the corresponding response
     // as a reply to any other request...
     if ($resp->getRequestHeader("authorization")) {
         return false;
     }
     return true;
 }
コード例 #2
0
 /**
  * Execute an HTTP Request
  *
  * @param Google_HttpRequest $request the http request to be executed
  * @return Google_HttpRequest http request with the response http code,
  * response headers and response body filled in
  * @throws Google_IO_Exception on curl or IO error
  */
 public function executeRequest(Google_Http_Request $request)
 {
     $curl = curl_init();
     if ($request->getPostBody()) {
         curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getPostBody());
     }
     $requestHeaders = $request->getRequestHeaders();
     if ($requestHeaders && is_array($requestHeaders)) {
         $curlHeaders = array();
         foreach ($requestHeaders as $k => $v) {
             $curlHeaders[] = "{$k}: {$v}";
         }
         curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders);
     }
     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod());
     curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent());
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_HEADER, true);
     curl_setopt($curl, CURLOPT_URL, $request->getUrl());
     if ($request->canGzip()) {
         curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
     }
     foreach ($this->options as $key => $var) {
         curl_setopt($curl, $key, $var);
     }
     if (!isset($this->options[CURLOPT_CAINFO])) {
         curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem');
     }
     $response = curl_exec($curl);
     if ($response === false) {
         throw new Google_IO_Exception(curl_error($curl));
     }
     $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
     $responseBody = substr($response, $headerSize);
     $responseHeaderString = substr($response, 0, $headerSize);
     $responseHeaders = $this->getHttpResponseHeaders($responseHeaderString);
     $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     return array($responseBody, $responseHeaders, $responseCode);
 }
コード例 #3
0
 /**
  * Decode an HTTP Response.
  * @static
  * @throws Google_Service_Exception
  * @param Google_Http_Request $response The http response to be decoded.
  * @return mixed|null
  */
 public static function decodeHttpResponse($response)
 {
     $code = $response->getResponseHttpCode();
     $body = $response->getResponseBody();
     $decoded = null;
     if (intVal($code) >= 300) {
         $decoded = json_decode($body, true);
         $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl();
         if (isset($decoded['error']) && isset($decoded['error']['message']) && isset($decoded['error']['code'])) {
             // if we're getting a json encoded error definition, use that instead of the raw response
             // body for improved readability
             $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}";
         } else {
             $err .= ": ({$code}) {$body}";
         }
         $errors = null;
         // Specific check for APIs which don't return error details, such as Blogger.
         if (isset($decoded['error']) && isset($decoded['error']['errors'])) {
             $errors = $decoded['error']['errors'];
         }
         throw new Google_Service_Exception($err, $code, null, $errors);
     }
     // Only attempt to decode the response, if the response code wasn't (204) 'no content'
     if ($code != '204') {
         $decoded = json_decode($body, true);
         if ($decoded === null || $decoded === "") {
             throw new Google_Service_Exception("Invalid json in service response: {$body}");
         }
         if ($response->getExpectedClass()) {
             $class = $response->getExpectedClass();
             $class = "RKuzovlev\\GoogleAPI\\Service\\" . $class;
             $decoded = new $class($decoded);
         }
     }
     return $decoded;
 }