This repository has been archived by the owner on Oct 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Instagram.php
216 lines (186 loc) · 4.63 KB
/
Instagram.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/**
* Instagram SDK
* A simple PHP SDK for Instagram API. Provides a wrapper for making authenticated requests.
*
* Instagram API Oficial Docs: https://instagram.com/developer/
*
* @package instagram-sdk
* @author Daniel Trolezi <[email protected]>
* @version 2.0.9
*/
class Instagram
{
/**
* @var string
*/
private $base_url = 'https://api.instagram.com/v1/';
/**
* @var string
*/
private $auth_url = 'https://api.instagram.com/oauth/authorize/';
/**
* @var string
*/
private $token_url = 'https://api.instagram.com/oauth/access_token/';
/**
* @var string
*/
protected $client_id;
/**
* @var string
*/
protected $client_secret;
/**
* @var string
*/
protected $redirect_uri;
/**
* @var string
*/
protected $access_token;
/**
* Constructor for the API.
*
* @param string $client_id
* @param string $client_secret
*/
function __construct($client_id, $client_secret)
{
$this->client_id = $client_id;
$this->client_secret = $client_secret;
}
/**
* Set the redirect uri, which is required to get
* the login url and get the access token.
*
* @param $redirect_uri
*/
public function setRedirectUri($redirect_uri)
{
$this->redirect_uri = $redirect_uri;
}
/**
* Set the Access Token.
*
* @param string $access_token
*/
public function setAccessToken($access_token)
{
$this->access_token = $access_token;
}
/**
* Return a valid Access Token or the one that is being used by the object.
*
* @param string $code
* @return string
* @throws Exception
* @throws InstagramException
*/
public function getAccessToken($code = null)
{
if($code){
$data = [
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirect_uri,
'code' => $code
];
$result = $this->curl($this->token_url, $data);
$result = json_decode($result, true);
if(isset($result['error_message'])) {
throw new InstagramException($result['error_message'], $result['error_type'], $result['code']);
}
$this->access_token = $result['access_token'];
return $this->access_token;
} else if($this->access_token){
return $this->access_token;
}
throw new Exception('You must provide the "code" resulting from the login webflow.', 400);
}
/**
* Returns the login URL.
*
* @param string $scope
* @return string
* @throws Exception
*/
public function getLoginURL($scope = 'basic')
{
if(!$this->redirect_uri) {
throw new Exception('You must provide a "redirect_uri".', 400);
}
return $this->auth_url.'?client_id='.$this->client_id.'&redirect_uri='.$this->redirect_uri.'&response_type=code&scope='.$scope;
}
/**
* Make authenticated requests to the API.
*
* @param string $endpoint - e.g: /users/{user-id|username|self}/media/recent
* @param array $params
* @return mixed
* @throws InstagramException
*/
public function call($endpoint, $params = array())
{
$url = $this->base_url . trim($endpoint, "/");
if($this->access_token) {
$params['access_token'] = $this->access_token;
}
$result = $this->curl($url.'?'.http_build_query($params));
$result = json_decode($result, true);
if(isset($result['meta']['error_message'])) {
throw new InstagramException($result['meta']['error_message'], $result['meta']['error_type'], $result['meta']['code']);
}
return $result;
}
/**
* @param string $url
* @param array $data
* @return mixed
*/
private function curl($url, $data = array())
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
if(isset($_SERVER["HTTP_USER_AGENT"])){
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
}
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: multipart/form-data"]);
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$result = curl_exec($ch);
//var_dump(curl_getinfo($ch));
curl_close($ch);
return $result;
}
}
class InstagramException extends Exception
{
/**
* @var string
*/
public $type;
public function __construct($message, $type, $code, \Exception $previous = null)
{
$this->type = $type;
parent::__construct($message, $code, $previous);
}
/**
* Gets the Exception error type
* @return string
*/
public function getType()
{
return $this->type;
}
}