Last active
June 17, 2019 15:04
-
-
Save cosenary/8803601 to your computer and use it in GitHub Desktop.
Instagram API login (with sessions)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require '../src/Instagram.php'; | |
use MetzWeb\Instagram\Instagram; | |
session_start(); | |
if (isset($_SESSION['access_token'])) { | |
// user authentication -> redirect to media | |
header('Location: success.php'); | |
} | |
// initialize class | |
$instagram = new Instagram(array( | |
'apiKey' => 'YOUR_APP_KEY', | |
'apiSecret' => 'YOUR_APP_SECRET', | |
'apiCallback' => 'YOUR_APP_CALLBACK' | |
)); | |
// create login URL | |
$loginUrl = $instagram->getLoginUrl(array( | |
'basic', | |
'likes', | |
'relationships' | |
)); | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Instagram - OAuth Login</title> | |
<link rel="stylesheet" type="text/css" href="assets/style.css"> | |
<style> | |
.login { | |
display: block; | |
font-size: 20px; | |
font-weight: bold; | |
margin-top: 50px; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<header class="clearfix"> | |
<h1>Instagram <span>display your photo stream</span></h1> | |
</header> | |
<div class="main"> | |
<ul class="grid"> | |
<li><img src="assets/instagram-big.png" alt="Instagram logo"></li> | |
<li> | |
<a class="login" href="<?php echo $loginUrl ?>">» Login with Instagram</a> | |
<h4>Use your Instagram account to login.</h4> | |
</li> | |
</ul> | |
<!-- GitHub project --> | |
<footer> | |
<p>created by <a href="https://github.com/cosenary/Instagram-PHP-API">cosenary's Instagram class</a>, available on GitHub</p> | |
</footer> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Instagram PHP API | |
* | |
* @link https://github.com/cosenary/Instagram-PHP-API | |
* @author Christian Metz | |
*/ | |
require '../src/Instagram.php'; | |
use MetzWeb\Instagram\Instagram; | |
// initialize class | |
$instagram = new Instagram(array( | |
'apiKey' => 'YOUR_APP_KEY', | |
'apiSecret' => 'YOUR_APP_SECRET', | |
'apiCallback' => 'YOUR_APP_CALLBACK' | |
)); | |
session_start(); | |
$token = false; | |
if (isset($_SESSION['access_token'])) { | |
// user authenticated -> receive and set token | |
$token = $_SESSION['access_token']; | |
} else { | |
// receive OAuth code parameter | |
$code = $_GET['code']; | |
// authentication in progress? | |
if (isset($code)) { | |
// receive and store OAuth token | |
$data = $instagram->getOAuthToken($code); | |
$token = $data->access_token; | |
$_SESSION['access_token'] = $token; | |
} else { | |
// check whether an error occurred | |
if (isset($_GET['error'])) { | |
echo 'An error occurred: ' . $_GET['error_description']; | |
} | |
} | |
} | |
// check authentication | |
if ($token === false) { | |
// authentication failed -> redirect to login | |
header('Location: index.php'); | |
} else { | |
// store user access token | |
$instagram->setAccessToken($token); | |
// now we have access to all authenticated user methods | |
$media = $instagram->getUserMedia(); | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Instagram - photo stream</title> | |
<link href="https://vjs.zencdn.net/4.2/video-js.css" rel="stylesheet"> | |
<link href="assets/style.css" rel="stylesheet"> | |
<script src="https://vjs.zencdn.net/4.2/video.js"></script> | |
</head> | |
<body> | |
<div class="container"> | |
<header class="clearfix"> | |
<img src="assets/instagram.png" alt="Instagram logo"> | |
<h1>Instagram photos <span>taken by <?php echo $data->user->username ?></span></h1> | |
</header> | |
<div class="main"> | |
<ul class="grid"> | |
<?php | |
// display all user likes | |
foreach ($result->data as $media) { | |
$content = "<li>"; | |
// output media | |
if ($media->type === 'video') { | |
// video | |
$poster = $media->images->low_resolution->url; | |
$source = $media->videos->standard_resolution->url; | |
$content .= "<video class=\"media video-js vjs-default-skin\" width=\"250\" height=\"250\" poster=\"{$poster}\" | |
data-setup='{\"controls\":true, \"preload\": \"auto\"}'> | |
<source src=\"{$source}\" type=\"video/mp4\" /> | |
</video>"; | |
} else { | |
// image | |
$image = $media->images->low_resolution->url; | |
$content .= "<img class=\"media\" src=\"{$image}\"/>"; | |
} | |
// create meta section | |
$avatar = $media->user->profile_picture; | |
$username = $media->user->username; | |
$comment = $media->caption->text; | |
$content .= "<div class=\"content\"> | |
<div class=\"avatar\" style=\"background-image: url({$avatar})\"></div> | |
<p>{$username}</p> | |
<div class=\"comment\">{$comment}</div> | |
</div>"; | |
// output media | |
echo $content . "</li>"; | |
} | |
?> | |
</ul> | |
<!-- GitHub project --> | |
<footer> | |
<p>created by <a href="https://github.com/cosenary/Instagram-PHP-API">cosenary's Instagram class</a>, available on GitHub</p> | |
<iframe width="95px" scrolling="0" height="20px" frameborder="0" allowtransparency="true" src="http://ghbtns.com/github-btn.html?user=cosenary&repo=Instagram-PHP-API&type=fork&count=true"></iframe> | |
</footer> | |
</div> | |
</div> | |
<!-- javascript --> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
<script> | |
$(document).ready(function() { | |
// rollover effect | |
$('li').hover( | |
function() { | |
var $media = $(this).find('.media'); | |
var height = $media.height(); | |
$media.stop().animate({ marginTop: -(height - 82) }, 1000); | |
}, function() { | |
var $media = $(this).find('.media'); | |
$media.stop().animate({ marginTop: '0px' }, 1000); | |
} | |
); | |
}); | |
</script> | |
</body> | |
</html> |
Guys, is there a way to make this happen without going to instagram login screen ? I would like to make a custom login screen .. User enter their instagram details and thats it. I need to create a webservice that can make this happen. Ive seen this working in app: famousgram . They have a custom login. You never get the instagram login scren...
Invalid argument supplied for foreach() xxx.com/success.php on line 69
Can you help me about this issue?
$media variable in 55. line need be $result
where instagram.php file?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @cosenary , how about checking status code for successful result .Also at line 73 you used $data object but returned result set is $media at line 55