Exemplo n.º 1
0
 /**
  * Execute an HTTP Request
  *
  * @param Request $request the http request to be executed
  * @return array containing response headers, body, and http code
  * @throws Exception on curl or IO error
  */
 public function executeRequest(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_URL, $request->getUrl());
     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);
     // The SSL version will be determined by the underlying library
     // @see https://github.com/google/google-api-php-client/pull/644
     //curl_setopt($curl, CURLOPT_SSLVERSION, 1);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_HEADER, true);
     if ($request->canGzip()) {
         curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
     }
     $options = $this->client->getClassConfig('Curl', 'options');
     if (is_array($options)) {
         $this->setOptions($options);
     }
     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');
     }
     $this->client->getLogger()->debug('cURL request', array('url' => $request->getUrl(), 'method' => $request->getRequestMethod(), 'headers' => $requestHeaders, 'body' => $request->getPostBody()));
     $response = curl_exec($curl);
     if ($response === false) {
         $error = curl_error($curl);
         $code = curl_errno($curl);
         $map = $this->client->getClassConfig('Exception', 'retry_map');
         $this->client->getLogger()->error('cURL ' . $error);
         throw new Exception($error, $code, null, $map);
     }
     $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
     list($responseHeaders, $responseBody) = $this->parseHttpResponse($response, $headerSize);
     $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     $this->client->getLogger()->debug('cURL response', array('code' => $responseCode, 'headers' => $responseHeaders, 'body' => $responseBody));
     return array($responseBody, $responseHeaders, $responseCode);
 }
Exemplo n.º 2
0
 /**
  * Revoke an OAuth2 access token or refresh token. This method will revoke the current access
  * token, if a token isn't provided.
  * @throws Exception
  * @param string|null $token The token (access token or a refresh token) that should be revoked.
  * @return boolean Returns True if the revocation was successful, otherwise False.
  */
 public function revokeToken($token = null)
 {
     if (!$token) {
         if (!$this->token) {
             // Not initialized, no token to actually revoke
             return false;
         } elseif (array_key_exists('refresh_token', $this->token)) {
             $token = $this->token['refresh_token'];
         } else {
             $token = $this->token['access_token'];
         }
     }
     $request = new Request(self::OAUTH2_REVOKE_URI, 'POST', array(), "token={$token}");
     $request->disableGzip();
     $response = $this->client->getIo()->makeRequest($request);
     $code = $response->getResponseHttpCode();
     if ($code == 200) {
         $this->token = null;
         return true;
     }
     return false;
 }
Exemplo n.º 3
0
 /**
  * Update a cached request, using the headers from the last response.
  * @param Request $cached A previously cached response.
  * @param mixed Associative array of response headers from the last request.
  */
 protected function updateCachedRequest($cached, $responseHeaders)
 {
     $hopByHop = self::$HOP_BY_HOP;
     if (!empty($responseHeaders['connection'])) {
         $connectionHeaders = array_map('strtolower', array_filter(array_map('trim', explode(',', $responseHeaders['connection']))));
         $hopByHop += array_fill_keys($connectionHeaders, true);
     }
     $endToEnd = array_diff_key($responseHeaders, $hopByHop);
     $cached->setResponseHeaders($endToEnd);
 }