Skip to content

Instantly share code, notes, and snippets.

@mymizan
Created February 20, 2016 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mymizan/1bb595b0ac4dc444d1c8 to your computer and use it in GitHub Desktop.
Save mymizan/1bb595b0ac4dc444d1c8 to your computer and use it in GitHub Desktop.
<?php
session_start();
require_once __DIR__ . "/vendor/autoload.php";
require_once __DIR__ . "/config.php";
//retrieve images
if (!empty($_SESSION['data'])){
$token = $_SESSION['data']['access_token'];
$client = new GuzzleHttp\Client();
$response = $client->request('GET', 'https://api.instagram.com/v1/users/self/media/recent/', array(
'query' => array(
'access_token' => $token,
'COUNT' => 99999999999,
'client_id' => CLIENT_ID,
)));
$body = (string) $response->getBody();
echo $body;
die;
}
//check Oauth flow
if (empty($_REQUEST['code']) ) {
$oauth_url = "https://api.instagram.com/oauth/authorize/?client_id=" . CLIENT_ID . "&redirect_uri=" . APPROOT . "/oauth.php&response_type=code";
header('Location: ' . $oauth_url);
//&scope=likes+comments+public_content+follower_list+relationships
die;
} else {
//request access token
$client = new GuzzleHttp\Client();
$response = $client->request('POST', 'https://api.instagram.com/oauth/access_token', array(
'form_params' => array(
'code' => $_REQUEST['code'],
'client_secret' => CLIENT_SECRET,
'client_id' => CLIENT_ID,
'grant_type' => 'authorization_code',
'redirect_uri' => APPROOT . "/oauth.php",
)));
if ($response->getStatusCode() == 200){
$body = $response->getBody();
$data = json_decode($body->getContents(), true);
//save access token
$_SESSION['data'] = $data;
echo $data['access_token'];
} else {
echo $response->getReasonPhrase();
echo (string) $response->getBody();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment