Skip to content

Instantly share code, notes, and snippets.

@AaronTraas
Last active November 10, 2015 02: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 AaronTraas/809b4d44e30d9523491e to your computer and use it in GitHub Desktop.
Save AaronTraas/809b4d44e30d9523491e to your computer and use it in GitHub Desktop.
Instagram auth test
<!DOCTYPE html>
<html lang="en">
<head>
<title>Instagram Auth Test</title>
</head>
<body>
<h1>Instagram Auth Test</h1>
<?php
// Register your app here: http://instagram.com/developer/
$client_id = 'YOUR_CLIENT_ID';
$client_secret = 'YOUR_CLIENT_SECRET';
$redirect_url = "YOUR_REDIRECT_URL";
// if the code parameter has been sent, we retrieve the access_token
if(isset($_GET['code']))
{
$code = $_GET['code'];
$url = "https://api.instagram.com/oauth/access_token";
$access_token_parameters = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'grant_type' => 'authorization_code',
'redirect_uri' => $redirect_url,
'code' => $code
);
$curl = curl_init( $url );
curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $access_token_parameters );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
$result = curl_exec( $curl );
curl_close( $curl );
$arr = json_decode( $result, true );
if( isset( $arr['access_token'] ) )
{
echo 'logged in user: ' . $arr['user']['username'];
}
else
{
echo 'Error: ' . $arr[error_message];
}
}
else
{
$oauth_url = "https://api.instagram.com/oauth/authorize/?client_id=$client_id&redirect_uri=$redirect_url&response_type=code";
echo "<a href=\"$oauth_url\">Log in to Instagram</a>";
}
?>
</body>
</html>
@AaronTraas
Copy link
Author

This is a very crude, single-source-file piece of code that tests authentication against the Instagram API. In order to run this, you must register your application here:

http://instagram.com/developer/

And update $client_id, $client_secret, and $redirect_url with the information provided by Instagram that is specific to your application.

@esmic
Copy link

esmic commented Nov 10, 2015

if i need to just get json results can i do a print of $arr (sorry very new to php)

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