/** * 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; }
/** * 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 */ public function retrieveResponse(UriInterface $endpoint, $requestBody, array $extraHeaders = array(), $method = 'POST') { $http = new \DokuHTTPClient(); $http->headers = array_merge($http->headers, $extraHeaders); $ok = $http->sendRequest($endpoint->getAbsoluteUri(), $requestBody, $method); if (!$ok) { throw new TokenResponseException($http->error); } return $http->resp_body; }
/** * {@inheritdoc} */ public function retrieveResponse(UriInterface $endpoint, $requestBody, array $extraHeaders = [], $method = 'POST') { if (!$this->client) { $this->client = new BaseHttpClient(); } $request = new Request(new Uri($endpoint->getAbsoluteUri())); $request->setMethod($method); if ($requestBody && is_array($requestBody)) { $request->setPosts($requestBody); } foreach ($extraHeaders as $name => $value) { $request->addHeader($name, $value); } $response = $this->client->send($request); return $response->getBody(); }
/** * @return string * @throws TokenResponseException */ protected function streams() { $this->prepareRequest(); $context = stream_context_create(array('http' => array('method' => $this->method, 'header' => array_values($this->extraHeaders), 'content' => $this->requestBody, 'protocol_version' => '1.1', 'user_agent' => 'Yii2 EAuth Client', 'max_redirects' => $this->maxRedirects, 'timeout' => $this->timeout))); $level = error_reporting(0); $response = file_get_contents($this->endpoint->getAbsoluteUri(), false, $context); error_reporting($level); if (YII_DEBUG) { Yii::trace('EAuth http response: ' . PHP_EOL . var_export($response, true), __NAMESPACE__); } if (false === $response) { $lastError = error_get_last(); if (is_null($lastError)) { $errStr = 'Failed to request resource.'; } else { $errStr = $lastError['message']; } Yii::error('EAuth streams error: ' . $errStr, __NAMESPACE__); throw new TokenResponseException($errStr); } return $response; }
/** * Builds the full URI based on all the properties * * @return string */ public function getCurrentUri() { return $this->currentUri->getAbsoluteUri(); }