├── src
├── config.php
└── Instagram.php
├── callback.php
├── README.md
└── index.php
/src/config.php:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/callback.php:
--------------------------------------------------------------------------------
1 | getAccessToken($_GET['code'], REDIRECT_URI);
19 | $_SESSION['access_token'] = $access_token->access_token;
20 |
21 | // Do what you want with the access token, maybe store it in a database?
22 | // Close window or redirect the user back to our index.php so we can pull in some data.
23 |
24 | header("Location: index.php");
25 | //exit('');
26 | ?>
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | #Instagram-OAuth
2 | *A PHP Class*
3 |
4 | ##Description
5 | **Author**: Mike Helmick
6 | **Last Edit**: May 28th, 2011
7 | **Version**: 1.0
8 |
9 |
10 | This is class written to work with Instagram OAuth methods. It authorizes a user and will let you make requests to Instagram API.
11 |
12 | ##Usage
13 | * Edit the CONFIG file in the 'src' folder to accommodate your *CLIENT_ID*, *CLIENT_SECRET* and *REDIRECT_URI*.
14 | * Customize callback.php to possibly save tokens to a database in case the user revisits your app.
15 | * Customize index.php to your liking and expand your app.
16 | * Happy coding!
17 |
18 | ##License
19 | This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
20 |
21 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
22 |
23 | You should have received a copy of the GNU General Public Licensealong with this program. If not, see .
--------------------------------------------------------------------------------
/index.php:
--------------------------------------------------------------------------------
1 | authorizeUrl(REDIRECT_URI, array('basic', 'comments', 'likes', 'relationships'));
16 | } else {
17 | try {
18 | $feed = $instagram->get('users/self/feed');
19 | }catch(InstagramApiError $e) {
20 | die($e->getMessage());
21 | }
22 | }
23 | ?>
24 |
25 |
26 | Log in
27 |
28 |
29 | data as $item): ?>
30 | -
31 | ID: id; ?>
32 | 
33 | By: user->username; ?>
34 |
35 |
36 |
37 |
38 |
39 | post('media/SOME-ID-HERE/likes');
43 | //$instagram->delete('media/SOME-ID-HERE/likes');
44 | }catch(InstagramApiError $e) {
45 | die($e->getMessage());
46 | }
47 | ?>
--------------------------------------------------------------------------------
/src/Instagram.php:
--------------------------------------------------------------------------------
1 | .
15 | */
16 |
17 | /**
18 | * This is class written to work with Instagram OAuth methods. It
19 | * authorizes a user and will let you make requests to Instagram API.
20 | *
21 | * Last Edit: May 28th, 2011
22 | * @author Mike Helmick (mikeh@ydekproductions.com)
23 | * @version 1.0
24 | *
25 | **/
26 |
27 | class Instagram {
28 | private $apiBase = 'https://api.instagram.com/';
29 | private $apiUrl = 'https://api.instagram.com/v1/';
30 |
31 | protected $client_id;
32 | protected $client_secret;
33 | protected $access_token;
34 |
35 | public function accessTokenUrl() { return $this->apiBase.'oauth/access_token/'; }
36 | public function authorizeUrl($redirect_uri, $scope = array('basic'), $response_type = 'code'){
37 | return $this->apiBase.'oauth/authorize/?client_id='.$this->client_id.'&redirect_uri='.$redirect_uri.'&response_type='.$response_type.'&scope='.implode('+', $scope);
38 | }
39 |
40 | public function __construct($client_id='', $client_secret='', $access_token = '')
41 | {
42 | if(empty($client_id) || empty($client_secret)){
43 | throw new Exception('You need to configure your Client ID and/or Client Secret keys.');
44 | }
45 | $this->client_id = $client_id;
46 | $this->client_secret = $client_secret;
47 | $this->access_token = $access_token;
48 | }
49 |
50 | private function urlEncodeParams($params)
51 | {
52 | $postdata = '';
53 | if(!empty($params)){
54 | foreach($params as $key => $value)
55 | {
56 | $postdata .= '&'.$key.'='.urlencode($value);
57 | }
58 | }
59 |
60 | return $postdata;
61 | }
62 |
63 | public function http($url, $params, $method)
64 | {
65 | $c = curl_init();
66 |
67 | // If they are authenticated and there is a access token passed, send it along with the request
68 | // If the access token is invalid, an error will be raised upon the request
69 | if($this->access_token){
70 | $url = $url.'?access_token='.$this->access_token;
71 | }
72 |
73 | // If the request is a GET and we need to pass along more params, "URL Encode" them.
74 | if($method == 'GET'){
75 | $url = $url.$this->urlEncodeParams($params);
76 | }
77 |
78 | curl_setopt($c, CURLOPT_URL, $url);
79 |
80 | if($method == 'POST'){
81 | curl_setopt($c, CURLOPT_POST, True);
82 | curl_setopt($c, CURLOPT_POSTFIELDS, $params);
83 | }
84 |
85 | if($method == 'DELETE'){
86 | curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'DELETE');
87 | }
88 |
89 | curl_setopt($c, CURLOPT_RETURNTRANSFER, True);
90 |
91 | $r = json_decode(curl_exec($c));
92 |
93 | // Throw an error if maybe an access token expired or wasn't right
94 | // or if an ID doesn't exist or something
95 | if(isset($r->meta->error_type)){
96 | throw new InstagramApiError('Error: '.$r->meta->error_message);
97 | }
98 | return $r;
99 | }
100 |
101 | // Giving you some easy functions (get, post, delete)
102 | public function get($endpoint, $params=array(), $method='GET'){
103 | return $this->http($this->apiUrl.$endpoint, $params, $method);
104 | }
105 |
106 | public function post($endpoint, $params=array(), $method='POST'){
107 | return $this->http($this->apiUrl.$endpoint, $params, $method);
108 | }
109 |
110 | public function delete($endpoint, $params=array(), $method='DELETE'){
111 | return $this->http($this->apiUrl.$endpoint, $params, $method);
112 | }
113 |
114 | public function getAccessToken($code, $redirect_uri, $grant_type = 'authorization_code'){
115 | $rsp = $this->http($this->accessTokenUrl(), array('client_id' => $this->client_id, 'client_secret' => $this->client_secret, 'grant_type' => $grant_type, 'redirect_uri' => $redirect_uri, 'code' => $code), 'POST');
116 |
117 | return $rsp;
118 | }
119 | }
120 |
121 | class InstagramApiError extends Exception {}
122 | ?>
--------------------------------------------------------------------------------