Skip to content

Commit 63921ac

Browse files
committed
Add Instagram example
1 parent cdf9e78 commit 63921ac

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

examples/instagram_popular_media.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
require '../src/Curl.class.php';
3+
4+
5+
define('INSTAGRAM_CLIENT_ID', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
6+
define('INSTAGRAM_CLIENT_SECRET', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
7+
8+
session_start();
9+
10+
$redirect_uri = implode('', array(
11+
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http',
12+
'://',
13+
$_SERVER['SERVER_NAME'],
14+
$_SERVER['SCRIPT_NAME'],
15+
));
16+
17+
if (isset($_GET['code'])) {
18+
$code = $_GET['code'];
19+
20+
$curl = new Curl();
21+
$curl->post('https://api.instagram.com/oauth/access_token', array(
22+
'client_id' => INSTAGRAM_CLIENT_ID,
23+
'client_secret' => INSTAGRAM_CLIENT_SECRET,
24+
'grant_type' => 'authorization_code',
25+
'redirect_uri' => $redirect_uri,
26+
'code' => $code,
27+
));
28+
29+
if ($curl->error) {
30+
echo $curl->response->error_type . ': ' . $curl->response->error_message . '<br />';
31+
echo '<a href="?">Try again?</a>';
32+
exit;
33+
}
34+
35+
$_SESSION['access_token'] = $curl->response->access_token;
36+
}
37+
38+
if (isset($_SESSION['access_token'])) {
39+
$curl = new Curl();
40+
$curl->get('https://api.instagram.com/v1/media/popular', array(
41+
'access_token' => $_SESSION['access_token'],
42+
));
43+
foreach ($curl->response->data as $media) {
44+
echo
45+
'<a href="' . $media->link . '" target="blank">' .
46+
'<img alt="" src="' . $media->images->thumbnail->url . '" />' .
47+
'</a>';
48+
}
49+
}
50+
else {
51+
header('Location: https://api.instagram.com/oauth/authorize/?' . http_build_query(array(
52+
'client_id' => INSTAGRAM_CLIENT_ID,
53+
'redirect_uri' => $redirect_uri,
54+
'response_type' => 'code',
55+
)));
56+
exit;
57+
}

0 commit comments

Comments
 (0)