Skip to content

Instantly share code, notes, and snippets.

@luizventurote
Forked from arturmamedov/instagram_api.php
Created September 12, 2016 22:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luizventurote/2bfab670b1a7e6e53e2daa6ada587468 to your computer and use it in GitHub Desktop.
Save luizventurote/2bfab670b1a7e6e53e2daa6ada587468 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));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment