Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Add access token support
Browse files Browse the repository at this point in the history
This closes #59
  • Loading branch information
vinkla committed Nov 8, 2017
1 parent 838aa8f commit c658747
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand All @@ -34,7 +35,7 @@
},
"extra": {
"branch-alias": {
"dev-develop": "7.0-dev"
"dev-develop": "8.0-dev"
}
},
"minimum-stability": "dev",
Expand Down
25 changes: 17 additions & 8 deletions src/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
*/
class Instagram
{
/**
* The access token.
*
* @var string
*/
protected $accessToken;

/**
* The http client.
*
Expand All @@ -42,38 +49,40 @@ 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();
}

/**
* 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;
}
}
17 changes: 14 additions & 3 deletions tests/InstagramTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}

0 comments on commit c658747

Please sign in to comment.