From c65874707f772d42e590134e2c228e4434bb1598 Mon Sep 17 00:00:00 2001 From: Vincent Klaiber Date: Wed, 8 Nov 2017 10:08:40 +0100 Subject: [PATCH] Add access token support This closes #59 --- CHANGELOG.md | 5 +++++ README.md | 18 ++++++++---------- composer.json | 7 ++++--- src/Instagram.php | 25 +++++++++++++++++-------- tests/InstagramTest.php | 17 ++++++++++++++--- 5 files changed, 48 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fda9624..f9c54a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # CHANGELOG +## 8.0.0 (released 2017-11-08) + +- Add access token support +- Remove public feeds support ([#59](https://github.com/vinkla/instagram/issues/59)) + ## 7.0.0 (released 2017-05-31) - Add httplug support diff --git a/README.md b/README.md index 37a8b36..7e6212d 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,10 @@ use Vinkla\Instagram\Instagram; // Create a new instagram instance. -$instagram = new Instagram(); +$instagram = new Instagram('access_token'); -// Fetch the media feed. -$instagram->get('jerryseinfeld'); +// Fetch the user's recent media feed. +$instagram->get(); ``` [![Build Status](https://img.shields.io/travis/vinkla/instagram/master.svg?style=flat)](https://travis-ci.org/vinkla/instagram) @@ -32,23 +32,21 @@ $ composer require vinkla/instagram php-http/message php-http/guzzle6-adapter ## Usage -First you need to create a new `Vinkla\Instagram\Instagram` instance. +First you need to create a new `Vinkla\Instagram\Instagram` instance and add your access token. If you don't want to create a Instagram API application you can use [Pixel Union's access token generator](http://instagram.pixelunion.net) for Instagram. ```php use Vinkla\Instagram\Instagram; -$instagram = new Instagram(); +$instagram = new Instagram('access_token'); ``` -To fetch the Instagram media data you may use the `get()` method. +To fetch the user's recent media data you may use the `get()` method. ```php -$instagram->get('jerryseinfeld'); +$instagram->get(); ``` -> **Note:** This will only work with public Instagram accounts. - -To [preview the JSON data](https://www.instagram.com/jerryseinfeld/media) you can [visit the page](https://www.instagram.com/jerryseinfeld/media) in your browser. +> **Note:** You can only fetch recent media from the given user's access token. ## License diff --git a/composer.json b/composer.json index 0a8146b..bb5c300 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "vinkla/instagram", "description": "An easy-to-use and simple Instagram package", "license": "MIT", - "keywords": ["instagram", "media", "feed"], + "keywords": ["instagram", "media", "feed", "recent"], "authors": [ { "name": "Vincent Klaiber", @@ -20,7 +20,8 @@ "require-dev": { "php-http/guzzle6-adapter": "^1.1", "php-http/message": "^1.5", - "phpunit/phpunit": "^6.3" + "php-http/mock-client": "^1.0", + "phpunit/phpunit": "^6.4" }, "autoload": { "psr-4": { @@ -34,7 +35,7 @@ }, "extra": { "branch-alias": { - "dev-develop": "7.0-dev" + "dev-develop": "8.0-dev" } }, "minimum-stability": "dev", diff --git a/src/Instagram.php b/src/Instagram.php index 615e8a6..1e2f116 100644 --- a/src/Instagram.php +++ b/src/Instagram.php @@ -25,6 +25,13 @@ */ class Instagram { + /** + * The access token. + * + * @var string + */ + protected $accessToken; + /** * The http client. * @@ -42,13 +49,15 @@ class Instagram /** * Create a new instagram instance. * + * @param string $accessToken * @param \Http\Client\HttpClient|null $httpClient * @param \Http\Message\RequestFactory|null $requestFactory * * @return void */ - public function __construct(HttpClient $httpClient = null, RequestFactory $requestFactory = null) + public function __construct(string $accessToken, HttpClient $httpClient = null, RequestFactory $requestFactory = null) { + $this->accessToken = $accessToken; $this->httpClient = $httpClient ?: HttpClientDiscovery::find(); $this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find(); } @@ -56,24 +65,24 @@ public function __construct(HttpClient $httpClient = null, RequestFactory $reque /** * Fetch the media items. * - * @param string $user - * * @throws \Vinkla\Instagram\InstagramException * * @return array */ - public function get(string $user): array + public function get(): array { - $uri = sprintf('https://www.instagram.com/%s/media/', $user); + $uri = sprintf('https://api.instagram.com/v1/users/self/media/recent?access_token=%s', $this->accessToken); $request = $this->requestFactory->createRequest('GET', $uri); $response = $this->httpClient->sendRequest($request); - if ($response->getStatusCode() === 404) { - throw new InstagramException(sprintf('The user [%s] wasn\'t found.', $user)); + if ($response->getStatusCode() === 400) { + $body = json_decode((string) $response->getBody()); + + throw new InstagramException($body->meta->error_message); } - return json_decode((string) $response->getBody())->items; + return json_decode((string) $response->getBody())->data; } } diff --git a/tests/InstagramTest.php b/tests/InstagramTest.php index d25233f..5e68f75 100644 --- a/tests/InstagramTest.php +++ b/tests/InstagramTest.php @@ -13,6 +13,8 @@ namespace Vinkla\Tests\Instagram; +use GuzzleHttp\Psr7\Response; +use Http\Mock\Client; use PHPUnit\Framework\TestCase; use Vinkla\Instagram\Instagram; use Vinkla\Instagram\InstagramException; @@ -26,16 +28,25 @@ class InstagramTest extends TestCase { public function testGet() { - $items = (new Instagram())->get('jerryseinfeld'); + $response = new Response(200, [], json_encode([ + 'data' => range(0, 19), + ])); + + $client = new Client(); + $client->addResponse($response); + + $instagram = new Instagram('jerryseinfeld', $client); + $items = $instagram->get(); $this->assertInternalType('array', $items); $this->assertCount(20, $items); } - public function testNotFound() + public function testError() { $this->expectException(InstagramException::class); + $this->expectExceptionMessage('The access_token provided is invalid.'); - (new Instagram())->get('imspeechlessihavenospeech'); + (new Instagram('imspeechlessihavenospeech'))->get(); } }