diff --git a/examples/change-user-agent.php b/examples/change-user-agent.php new file mode 100644 index 00000000..9c7b9778 --- /dev/null +++ b/examples/change-user-agent.php @@ -0,0 +1,36 @@ +push( + Middleware::log( + $logger, + new MessageFormatter('{request}') + ) +); + +$client = new \FeedIo\Adapter\Guzzle\Client( + new GuzzleHttp\Client([ + 'handler' => $stack + ]), + 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36' +); + +$feedIo = new \FeedIo\FeedIo($client, $logger); + +$result = $feedIo->read('http://php.net/feed.atom'); + +echo "feed title : {$result->getFeed()->getTitle()} \n "; + +$client->setUserAgent('Another User Agent'); + +$feedIo->read('http://php.net/feed.atom'); + +echo "feed title : {$result->getFeed()->getTitle()} \n "; diff --git a/src/FeedIo/Adapter/Guzzle/Client.php b/src/FeedIo/Adapter/Guzzle/Client.php index 66967453..2cab80a9 100644 --- a/src/FeedIo/Adapter/Guzzle/Client.php +++ b/src/FeedIo/Adapter/Guzzle/Client.php @@ -24,17 +24,41 @@ */ class Client implements ClientInterface { + + /** + * Default user agent provided with the package + */ + const DEFAULT_USER_AGENT = 'Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1'; + /** * @var \GuzzleHttp\ClientInterface */ protected $guzzleClient; + /** + * @var string + */ + protected $userAgent; + /** * @param \GuzzleHttp\ClientInterface $guzzleClient + * @param string $userAgent */ - public function __construct(\GuzzleHttp\ClientInterface $guzzleClient) + public function __construct(\GuzzleHttp\ClientInterface $guzzleClient, string $userAgent = self::DEFAULT_USER_AGENT) { $this->guzzleClient = $guzzleClient; + $this->userAgent = $userAgent; + } + + /** + * @param string $userAgent The new user-agent + * @return Client + */ + public function setUserAgent(string $userAgent) : Client + { + $this->userAgent = $userAgent; + + return $this; } /** @@ -113,7 +137,7 @@ protected function getOptions(\DateTime $modifiedSince) : array { return [ 'headers' => [ - 'User-Agent' => 'Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1', + 'User-Agent' => $this->userAgent, 'If-Modified-Since' => $modifiedSince->format(\DateTime::RFC2822) ] ]; diff --git a/tests/FeedIo/Adapter/Guzzle/ClientTest.php b/tests/FeedIo/Adapter/Guzzle/ClientTest.php index e81efb53..05c49d6d 100644 --- a/tests/FeedIo/Adapter/Guzzle/ClientTest.php +++ b/tests/FeedIo/Adapter/Guzzle/ClientTest.php @@ -43,6 +43,13 @@ public function testGetResponse() $this->assertEquals(1994, $response->getLastModified()->format('Y')); } + public function testSetUserAgent() + { + $this->object->setUserAgent('the new user-agent'); + + $this->assertAttributeEquals('the new user-agent', 'userAgent', $this->object); + } + /** * @expectedException \FeedIo\Adapter\NotFoundException */ diff --git a/tests/FeedIo/FeedIoTest.php b/tests/FeedIo/FeedIoTest.php index 7b2bbfd8..88bd8778 100644 --- a/tests/FeedIo/FeedIoTest.php +++ b/tests/FeedIo/FeedIoTest.php @@ -139,8 +139,7 @@ public function testSetReader() $logger = new \Psr\Log\NullLogger(); $reader = new Reader( new Adapter\Guzzle\Client( - new \GuzzleHttp\Client(), - $logger + new \GuzzleHttp\Client() ), $logger );