/** * Calls an URL given a context. * * @param string $url An url. * @param resource $context A resource (created from stream_context_create). * * @throws \Widop\HttpAdapterBundle\Exception\HttpAdapterException If an error occured. * * @return @return \Widop\HttpAdapter\HttpResponse The response. */ private function execute($url, $context) { if (($stream = @fopen($this->fixUrl($url), 'rb', false, $context)) === false) { throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), print_r(error_get_last(), true)); } $metadata = stream_get_meta_data($stream); if (preg_match_all('#Location:([^,]+)#', implode(',', $metadata['wrapper_data']), $matches)) { $effectiveUrl = trim($matches[1][count($matches[1]) - 1]); } else { $effectiveUrl = $url; } $content = stream_get_contents($stream); fclose($stream); return $this->createResponse(isset($metadata['wrapper_data'][0]) ? $this->parseStatusCode($metadata['wrapper_data'][0]) : null, $url, $metadata['wrapper_data'], $content, $effectiveUrl); }
/** * Sends a request. * * @param string $url The url. * @param string $method The http method. * @param array $headers The headers. * @param array $content The content. * @param array $files The files. * * @throws \Widop\HttpAdapter\HttpAdapterException If an error occured. * * @return \Widop\HttpAdapter\HttpResponse The response. */ private function sendRequest($url, $method, array $headers = array(), array $content = array(), array $files = array()) { $this->client->resetParameters()->setOptions(array('maxredirects' => $this->getMaxRedirects()))->setMethod($method)->setUri($url)->setHeaders($headers)->setParameterPost($content); foreach ($files as $key => $file) { $this->client->setFileUpload($file, $key); } try { $response = $this->client->send(); } catch (\Exception $e) { throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $e->getMessage()); } return $this->createResponse($response->getStatusCode(), $url, $response->getHeaders()->toArray(), $method === Request::METHOD_HEAD ? '' : $response->getBody()); }
/** * Sends a request. * * @param string $url The url. * @param string $method The http method. * @param array $headers The header. * @param array $content The content. * * @throws \Widop\HttpAdapter\HttpAdapterException If an error occured. * * @return \Widop\HttpAdapter\HttpResponse The response. */ private function sendRequest($url, $method, array $headers = array(), array $content = array()) { $this->browser->getClient()->setMaxRedirects($this->getMaxRedirects()); try { $response = $this->browser->call($url, $method, $headers, $content); } catch (\Exception $e) { throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $e->getMessage()); } return $this->createResponse($response->getStatusCode(), $url, $response->getHeaders(), $response->getContent()); }
/** * Sends a request. * * @param \Guzzle\Http\Message\RequestInterface $request The request. * * @throws \Widop\HttpAdapter\HttpAdapterException If an error occured. * * @return \Widop\HttpAdapter\HttpResponse The response. */ private function sendRequest(RequestInterface $request) { $request->getParams()->set('redirect.max', $this->getMaxRedirects()); try { $response = $request->send(); } catch (\Exception $e) { throw HttpAdapterException::cannotFetchUrl($request->getUrl(), $this->getName(), $e->getMessage()); } return $this->createResponse($response->getStatusCode(), $request->getUrl(), $response->getHeaders()->toArray(), $response->getBody(true), $response->getEffectiveUrl()); }