/** * Constructor override */ public function __construct(Collection $config) { $this->setConfig($config); $this->setBaseUrl($config->get(Options::BASE_URL)); $this->defaultHeaders = new Collection(); $this->setRequestFactory(RequestFactory::getInstance()); }
/** * @param string $baseUrl Base URL of the web service * @param array|Collection $config Configuration settings * * @throws RuntimeException if cURL is not installed */ public function __construct($baseUrl = '', $config = null) { if (!extension_loaded('curl')) { // @codeCoverageIgnoreStart throw new RuntimeException('The PHP cURL extension must be installed to use Guzzle.'); // @codeCoverageIgnoreEnd } $this->setConfig($config ?: new Collection()); $this->initSsl(); $this->setBaseUrl($baseUrl); $this->defaultHeaders = new Collection(); $this->setRequestFactory(RequestFactory::getInstance()); $this->userAgent = $this->getDefaultUserAgent(); if (!$this->config[self::DISABLE_REDIRECTS]) { $this->addSubscriber(new RedirectPlugin()); } }
private function createPresignedRequest(RequestInterface $request, CredentialsInterface $credentials) { $sr = RequestFactory::getInstance()->cloneRequestWithMethod($request, 'GET'); // Move POST fields to the query if they are present if ($request instanceof EntityEnclosingRequestInterface) { foreach ($request->getPostFields() as $name => $value) { $sr->getQuery()->set($name, $value); } } // Make sure to handle temporary credentials if ($token = $credentials->getSecurityToken()) { $sr->setHeader('X-Amz-Security-Token', $token); $sr->getQuery()->set('X-Amz-Security-Token', $token); } $this->moveHeadersToQuery($sr); return $sr; }
/** * Create a redirect request for a specific request object * * Takes into account strict RFC compliant redirection (e.g. redirect POST with POST) vs doing what most clients do * (e.g. redirect POST with GET). * * @param RequestInterface $request Request being redirected * @param RequestInterface $original Original request * @param int $statusCode Status code of the redirect * @param string $location Location header of the redirect * * @return RequestInterface Returns a new redirect request * @throws CouldNotRewindStreamException If the body needs to be rewound but cannot */ protected function createRedirectRequest(RequestInterface $request, $statusCode, $location, RequestInterface $original) { $redirectRequest = null; $strict = $original->getParams()->get(self::STRICT_REDIRECTS); // Switch method to GET for 303 redirects. 301 and 302 redirects also switch to GET unless we are forcing RFC // compliance to emulate what most browsers do. NOTE: IE only switches methods on 301/302 when coming from a POST. if ($request instanceof EntityEnclosingRequestInterface && ($statusCode == 303 || !$strict && $statusCode <= 302)) { $redirectRequest = RequestFactory::getInstance()->cloneRequestWithMethod($request, 'GET'); } else { $redirectRequest = clone $request; } $redirectRequest->setIsRedirect(true); // Always use the same response body when redirecting $redirectRequest->setResponseBody($request->getResponseBody()); $location = Url::factory($location); // If the location is not absolute, then combine it with the original URL if (!$location->isAbsolute()) { $originalUrl = $redirectRequest->getUrl(true); // Remove query string parameters and just take what is present on the redirect Location header $originalUrl->getQuery()->clear(); $location = $originalUrl->combine((string) $location, true); } $redirectRequest->setUrl($location); // Add the parent request to the request before it sends (make sure it's before the onRequestClone event too) $redirectRequest->getEventDispatcher()->addListener('request.before_send', $func = function ($e) use(&$func, $request, $redirectRequest) { $redirectRequest->getEventDispatcher()->removeListener('request.before_send', $func); $e['request']->getParams()->set(RedirectPlugin::PARENT_REQUEST, $request); }); // Rewind the entity body of the request if needed if ($redirectRequest instanceof EntityEnclosingRequestInterface && $redirectRequest->getBody()) { $body = $redirectRequest->getBody(); // Only rewind the body if some of it has been read already, and throw an exception if the rewind fails if ($body->ftell() && !$body->rewind()) { throw new CouldNotRewindStreamException('Unable to rewind the non-seekable entity body of the request after redirecting. cURL probably ' . 'sent part of body before the redirect occurred. Try adding acustom rewind function using on the ' . 'entity body of the request using setRewindFunction().'); } } return $redirectRequest; }