From 8b16ed4f582fd8874c3c5897d4345ffb3742534a Mon Sep 17 00:00:00 2001 From: Hannes Giesenow Date: Tue, 7 Jun 2016 11:38:25 +0200 Subject: [PATCH] Added guzzle6 adapter --- src/Widop/HttpAdapter/Guzzle6HttpAdapter.php | 79 ++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/Widop/HttpAdapter/Guzzle6HttpAdapter.php diff --git a/src/Widop/HttpAdapter/Guzzle6HttpAdapter.php b/src/Widop/HttpAdapter/Guzzle6HttpAdapter.php new file mode 100644 index 0000000..894b671 --- /dev/null +++ b/src/Widop/HttpAdapter/Guzzle6HttpAdapter.php @@ -0,0 +1,79 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Widop\HttpAdapter; + +use GuzzleHttp\Client; +use GuzzleHttp\ClientInterface; + +/** + * Guzzle Http adapter. + * + * @author Gunnar Lium + */ +class Guzzle6HttpAdapter extends AbstractHttpAdapter +{ + /** @var ClientInterface */ + private $client; + + /** + * Creates a guzzle adapter. + * + * @param ClientInterface $client The guzzle client. + * @param integer $maxRedirects The maximum redirects. + */ + public function __construct(ClientInterface $client = null, $maxRedirects = 5) + { + parent::__construct($maxRedirects); + + if ($client === null) { + $client = new Client(array('allow_redirects' => array('max' => $maxRedirects))); + } + + $this->client = $client; + } + + /** + * {@inheritdoc} + */ + public function getContent($url, array $headers = array()) + { + try { + $response = $this->client->get($url, $headers); + + return $response->getBody(); + } catch (\Exception $e) { + throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $e->getMessage()); + } + } + + /** + * {@inheritdoc} + */ + public function postContent($url, array $headers = array(), $content = '') + { + try { + $response = $this->client->post($url, array('headers' => $headers, 'form_params' => $content)); + + return $response->getBody(); + } catch (\Exception $e) { + throw HttpAdapterException::cannotFetchUrl($url, $this->getName(), $e->getMessage()); + } + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return 'guzzle6'; + } +}