Example #1
0
 /**
  * Any implementing HTTP providers should send a request to the provided endpoint with the parameters.
  * They should return, in string form, the response body and throw an exception on error.
  *
  * @param UriInterface $endpoint
  * @param mixed        $requestBody
  * @param array        $extraHeaders
  * @param string       $method
  *
  * @return string
  *
  * @throws TokenResponseException
  * @throws \InvalidArgumentException
  */
 public function retrieveResponse(UriInterface $endpoint, $requestBody, array $extraHeaders = array(), $method = 'POST')
 {
     // Normalize method name
     $method = strtoupper($method);
     $this->normalizeHeaders($extraHeaders);
     if ($method === 'GET' && !empty($requestBody)) {
         throw new \InvalidArgumentException('No body expected for "GET" request.');
     }
     if (!isset($extraHeaders['Content-type']) && $method === 'POST' && is_array($requestBody)) {
         $extraHeaders['Content-type'] = 'Content-type: application/x-www-form-urlencoded';
     }
     $extraHeaders['Host'] = 'Host: ' . $endpoint->getHost();
     $extraHeaders['Connection'] = 'Connection: close';
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $endpoint->getAbsoluteUri());
     if ($method === 'POST' || $method === 'PUT') {
         if ($requestBody && is_array($requestBody)) {
             $requestBody = http_build_query($requestBody, '', '&');
         }
         if ($method === 'PUT') {
             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
         } else {
             curl_setopt($ch, CURLOPT_POST, true);
         }
         curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
     } else {
         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
     }
     if ($this->maxRedirects > 0) {
         // Handle open_basedir & safe mode
         if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
         }
         curl_setopt($ch, CURLOPT_MAXREDIRS, $this->maxRedirects);
     }
     curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_HEADER, false);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $extraHeaders);
     curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
     foreach ($this->parameters as $key => $value) {
         curl_setopt($ch, $key, $value);
     }
     if ($this->forceSSL3) {
         curl_setopt($ch, CURLOPT_SSLVERSION, 3);
     }
     $response = curl_exec($ch);
     $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     if (false === $response) {
         $errNo = curl_errno($ch);
         $errStr = curl_error($ch);
         curl_close($ch);
         if (empty($errStr)) {
             throw new TokenResponseException('Failed to request resource.', $responseCode);
         }
         throw new TokenResponseException('cURL Error # ' . $errNo . ': ' . $errStr, $responseCode);
     }
     curl_close($ch);
     return $response;
 }
 /**
  * Any implementing HTTP providers should send a request to the provided endpoint with the parameters.
  * They should return, in string form, the response body and throw an exception on error.
  *
  * @param UriInterface $endpoint
  * @param mixed        $requestBody
  * @param array        $extraHeaders
  * @param string       $method
  *
  * @return string
  *
  * @throws TokenResponseException
  * @throws \InvalidArgumentException
  */
 public function retrieveResponse(UriInterface $endpoint, $requestBody, array $extraHeaders = array(), $method = 'POST')
 {
     // Normalize method name
     $method = strtoupper($method);
     $this->normalizeHeaders($extraHeaders);
     if ($method === 'GET' && !empty($requestBody)) {
         throw new \InvalidArgumentException('No body expected for "GET" request.');
     }
     if (!isset($extraHeaders['Content-type']) && $method === 'POST' && is_array($requestBody)) {
         $extraHeaders['Content-type'] = 'Content-type: application/x-www-form-urlencoded';
     }
     $host = 'Host: ' . $endpoint->getHost();
     // Append port to Host if it has been specified
     if ($endpoint->hasExplicitPortSpecified()) {
         $host .= ':' . $endpoint->getPort();
     }
     $extraHeaders['Host'] = $host;
     $extraHeaders['Connection'] = 'Connection: close';
     if (is_array($requestBody)) {
         $requestBody = http_build_query($requestBody, '', '&');
     }
     $extraHeaders['Content-length'] = 'Content-length: ' . strlen($requestBody);
     $context = $this->generateStreamContext($requestBody, $extraHeaders, $method);
     $level = error_reporting(0);
     $response = file_get_contents($endpoint->getAbsoluteUri(), false, $context);
     error_reporting($level);
     if (false === $response) {
         $lastError = error_get_last();
         if (is_null($lastError)) {
             throw new TokenResponseException('Failed to request resource.');
         }
         throw new TokenResponseException($lastError['message']);
     }
     return $response;
 }
Example #3
0
 /**
  *
  */
 protected function prepareRequest()
 {
     $this->method = strtoupper($this->method);
     $this->normalizeHeaders($this->extraHeaders);
     if ($this->method === 'GET' && !empty($this->requestBody)) {
         throw new \InvalidArgumentException('No body expected for "GET" request.');
     }
     if (!isset($this->extraHeaders['Content-type']) && $this->method === 'POST' && is_array($this->requestBody)) {
         $this->extraHeaders['Content-type'] = 'Content-type: application/x-www-form-urlencoded';
     }
     // Some of services requires User-Agent header (e.g. GitHub)
     if (!isset($this->extraHeaders['User-Agent'])) {
         $this->extraHeaders['User-Agent'] = 'User-Agent: yii2-eauth';
     }
     $this->extraHeaders['Host'] = 'Host: ' . $this->endpoint->getHost();
     $this->extraHeaders['Connection'] = 'Connection: close';
     if (YII_DEBUG) {
         Yii::trace('EAuth http request: ' . PHP_EOL . var_export(array('url' => $this->endpoint->getAbsoluteUri(), 'method' => $this->method, 'headers' => $this->extraHeaders, 'body' => $this->requestBody), true), __NAMESPACE__);
     }
     if (is_array($this->requestBody)) {
         $this->requestBody = http_build_query($this->requestBody, null, '&');
     }
 }