Skip to content

Instantly share code, notes, and snippets.

@LennardWesterveld
Created February 21, 2018 13:31
Show Gist options
  • Save LennardWesterveld/722d3d80a10182609d5362c1d417e43a to your computer and use it in GitHub Desktop.
Save LennardWesterveld/722d3d80a10182609d5362c1d417e43a to your computer and use it in GitHub Desktop.
InstagramClient Drupal
<?php
namespace Drupal\ko_social_feed_instagram;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Vinkla\Instagram\InstagramException;
/**
* {@inheritdoc}
*/
class InstagramClient {
const INSTAGRAM_API_URL = 'https://api.instagram.com/';
/**
* The access token.
*
* @var string
*/
protected $accessToken;
/**
* The http client.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* Create a new Instagram instance.
*
* @param string $accessToken
* Instagram accessToken.
*/
public function __construct(string $accessToken) {
$this->accessToken = $accessToken;
$this->httpClient = \Drupal::httpClient();
}
/**
* Fetch the media items.
*
* @throws \Vinkla\Instagram\InstagramException
*
* @return array
* return response.
*/
public function getRecentMedia($max_id = NULL, $min_id = NULL, $count = NULL): array {
$response = $this->httpClient->get(self::INSTAGRAM_API_URL . 'v1/users/self/media/recent',
[
'query' =>
[
'access_token' => $this->accessToken,
// Is not working for now.
// TODO: fix later on when have clearns from Instagram.
// 'max_id' => $max_id,
// 'min_id' => $min_id,.
'count' => $count,
],
]);
if ($response->getStatusCode() === 400) {
$body = json_decode((string) $response->getBody());
throw new InstagramException($body->meta->error_message);
}
return json_decode((string) $response->getBody())->data;
}
/**
* Start auth code request / redirect.
*
* @param string $client_id
* Client id.
* @param string $redirect_uri
* Valid Instagram redirect uri.
*/
public static function authenticate($client_id, $redirect_uri) {
$url = Url::fromUri(self::INSTAGRAM_API_URL . 'oauth/authorize', [
'query' => [
'client_id' => $client_id,
'response_type' => 'code',
'redirect_uri' => $redirect_uri,
],
]);
$response = new RedirectResponse($url->toString(), 302);
$response->send();
}
/**
* Retrieve access token.
*
* @param string $code
* Code from authenticate call.
* @param string $client_id
* Client id.
* @param string $client_secret
* Client secret.
* @param string $redirect_uri
* Valid Instagram redirect uri.
*
* @return array|object
* Response.
*/
public static function retrieveAccessToken($code, $client_id, $client_secret, $redirect_uri) {
$client = \Drupal::httpClient();
$response = $client->post(self::INSTAGRAM_API_URL . 'oauth/access_token', [
'form_params' => [
'client_id' => $client_id,
'client_secret' => $client_secret,
'grant_type' => 'authorization_code',
'redirect_uri' => $redirect_uri,
'code' => $code,
],
]);
return json_decode($response->getBody());
}
}
@LennardWesterveld
Copy link
Author

LennardWesterveld commented Feb 21, 2018

to get big size images use the following:

$url = parse_url($data->images->standard_resolution->url);
$fileName = basename($data->images->standard_resolution->url);
$bigsize_image = $url['schema'] . '//' . $url['host'] . '/vp/' . $fileName;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment