/**
  * 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 Symfony\Component\HttpFoundation\Response
  *
  * @throws TokenResponseException
  * @throws \InvalidArgumentException
  */
 public function retrieveResponse(UriInterface $endpoint, $requestBody, array $extraHeaders = [], $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';
     }
     if ($endpoint->isDefaultPort()) {
         $extraHeaders['Host'] = 'Host: ' . $endpoint->getHost() . ':' . $endpoint->getPort();
     } else {
         $extraHeaders['Host'] = 'Host: ' . $endpoint->getHost();
     }
     $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);
     $body = file_get_contents($endpoint->getAbsoluteUri(), false, $context);
     error_reporting($level);
     if (false === $body) {
         $lastError = error_get_last();
         if (is_null($lastError)) {
             throw new RequestException('Failed to request resource.');
         }
         throw new RequestException($lastError['message']);
     }
     // -------------------------------------------------------------------------
     // Gponster <*****@*****.**> 2013/12/19
     // See https://github.com/Lusitanian/PHPoAuthLib/issues/144
     // -------------------------------------------------------------------------
     $httpResponseHeaders = [];
     list($version, $status, $reason) = explode(' ', $httpResponseHeaders[0], 3);
     $response = new Response($body, $status, $httpResponseHeaders);
     if ((int) $status === 200) {
         return $response;
     } else {
         $e = new ResponseException($reason);
         $e->setResponse($response);
         throw $e;
     }
 }
Exemple #2
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 Symfony\Component\HttpFoundation\Response
  *
  * @throws ResponseException
  * @throws RequestException
  * @throws \InvalidArgumentException
  */
 public function retrieveResponse(UriInterface $endpoint, $requestBody, array $extraHeaders = [], $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';
     }
     if ($endpoint->isDefaultPort()) {
         $extraHeaders['Host'] = 'Host: ' . $endpoint->getHost() . ':' . $endpoint->getPort();
     } else {
         $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) {
         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, true);
     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);
     }
     $data = curl_exec($ch);
     $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     if (false === $data) {
         $errNo = curl_errno($ch);
         $errStr = curl_error($ch);
         curl_close($ch);
         if (empty($errStr)) {
             throw new RequestException('Failed to request resource.', $status);
         }
         throw new RequestException('cURL Error # ' . $errNo . ': ' . $errStr, $status);
     }
     curl_close($ch);
     list($headers, $body) = explode('\\r\\n\\r\\n', $data, 2);
     $parsedHeaders = http_parse_headers($headers);
     $response = new Response($body, $status, $parsedHeaders);
     if ((int) $status === 200) {
         return $response;
     } else {
         $e = new ResponseException('HTTP status code $status');
         $e->setResponse($response);
         throw $e;
     }
 }