Skip to content

Instantly share code, notes, and snippets.

@arturmamedov
Created June 15, 2016 12:52
Show Gist options
  • Save arturmamedov/7f5a90b85a20e06e344ebb88dc898d25 to your computer and use it in GitHub Desktop.
Save arturmamedov/7f5a90b85a20e06e344ebb88dc898d25 to your computer and use it in GitHub Desktop.
Instagram API retrive access token with PHP curl
<?php
// yo can follow this guide to http://www.benrlodge.com/blog/post/how-to-generate-an-instagram-access-token#
#1 first you need to create a Client in Instgram Developer Dashboard
// https://www.instagram.com/developer/clients/manage/
#2 after you need to retrive a oAuth code for after get access_token
// https://www.instagram.com/developer/authentication/
// change the params with your one and open link in browser
// https://www.instagram.com/oauth/authorize/?client_id=YOUR_CLIENT_ID_GOES_HERE&redirect_uri=THAT_REDIRECT_URI_YOU_GAVE&response_type=code
// at this point you have a code ex: http://www.YOUR_REDIRECT_LINK.com?code=asdf4a0c15d80bc54ddea32d6f1751
// we need the code "asdf4a0c15d80bc54ddea32d6f1751"
# for use it in pour CURL request and obtain a access_token
// curl native commands
//curl -F 'client_id=CLIENT_ID' \
// -F 'client_secret=CLIENT_SECRET' \
// -F 'grant_type=authorization_code' \
// -F 'redirect_uri=AUTHORIZATION_REDIRECT_URI' \
// -F 'code=CODE' \
//
/// curl with PHP
$uri = 'https://api.instagram.com/oauth/access_token';
$data = [
'client_id' => '213esdaedasdasd12...YOUR_CLINT_ID',
'client_secret' => 'a8b4aaf06c0da310...YOUR_CLIENT_SECRET',
'grant_type' => 'authorization_code',
'redirect_uri' => 'http://www.YOUR_REDIRECT_URL.it',
'code' => 'asdf4a0c15d80bc54ddea32d6f1751...retrivedCODE'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri); // uri
curl_setopt($ch, CURLOPT_POST, true); // POST
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // POST DATA
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // RETURN RESULT true
curl_setopt($ch, CURLOPT_HEADER, 0); // RETURN HEADER false
curl_setopt($ch, CURLOPT_NOBODY, 0); // NO RETURN BODY false / we need the body to return
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // VERIFY SSL HOST false
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // VERIFY SSL PEER false
$result = json_decode(curl_exec($ch)); // execute curl
echo '<pre>'; // preformatted view
// ecit directly the result
exit(print_r($result));
?>
@arturmamedov
Copy link
Author

arturmamedov commented Jun 15, 2016

And after you can use an Instagram API library for retrive your pics:
jQInstaPics - https://jsfiddle.net/arturmamedov/c89sdhbd/
instafeed.js - https://jsfiddle.net/arturmamedov/om3mnm09/

@luizventurote
Copy link

Thanks arturmamedov. It was very helpful.

@bookchiq
Copy link

Thanks for sharing this. I've dabbled in command line curl but wasn't sure how to do the equivalent in PHP so this was exceptionally helpful.

@LSK-01
Copy link

LSK-01 commented Aug 11, 2017

acc thank you so so much this worked perfectly

@FullStackAlex
Copy link

молоток! :)

@Selvasan02
Copy link

I have tried But I am getting error like

stdClass Object
(
[error_type] => OAuthException
[code] => 400
[error_message] => Invalid Client ID
)
1

@Selvasan02
Copy link

Please advise

@arturmamedov
Copy link
Author

arturmamedov commented Feb 1, 2019

Please advise

Hi @Selvasan02 maybe instagram disable something i don't know, i pass to a more automated solution that permitt instagram photos from username, hashtag or location in any combination see example on this website https://www.hotelcityrimini.com/

For get it you need to subscribe a plan here https://goo.gl/dBnGNo

https://elfsight.com/?ref=d3805a92-b32f-41e2-890c-40d0422384f5

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