-
Notifications
You must be signed in to change notification settings - Fork 0
/
Instagram.php
318 lines (284 loc) · 8.81 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
/*
Library retrieve instagram search by hashtag
File name: Instagram.php
Author: areoid - [email protected]
Example:
$insta = new Instagram();
$result = $insta->setMaxData(10)
->setHashtag('sexy')
->retrieve();
OR get like
$likes = new Instagram();
$result = $likes->getLikesByInstagramId('POST_INSTA_ID')
->retrieve();
**/
class Instagram
{
private $_hashtag,
$_error = false,
$_error_message = '',
$_cliend_id = 'YOUR CLIENT ID',
$_access_token = 'YOUR ACCESS TOKEN',
$_max_data = NULL,
$_most_liked = false,
$_get_likes = false,
$_instagram_id = '',
$_result = [],
$_order_by_liked = NULL;
/*
* Optional method
* for initialization sorting data based on liked
* @param as string with value that allowed is
* asc, ASC, desc, or DESC
**/
public function orderByLiked($value = '')
{
if(!empty($value))
{
if($value == 'asc' || $value == 'ASC' || $value == 'desc' || $value == 'DESC')
{
$this->_order_by_liked = $value;
}
else
{
$this->_error = true;
$this->_error_message = 'Sorry, @param of orderByLiked() that allowed is asc, ASC, desc, or DESC';
}
}
else
{
$this->_error = true;
$this->_error_message = 'Sorry, @param of orderByLiked() can\'t with empty value';
}
return $this;
}
/*
* Optional method
* for initialization mostliked data
**/
public function mostLiked()
{
$this->_most_liked = true;
return $this;
}
/*
* Optional method
* initialization max data that needed
**/
public function setMaxData($maxdata)
{
if(!empty($maxdata))
{
if(is_numeric($maxdata))
{
// set @var _max_data
$this->_max_data = $maxdata;
}
else
{
$this->_error = true;
$this->_error_message = "Sorry, @param setMaxData() only integer";
}
}
else
{
$this->_error = true;
$this->_error_message = "Sorry, @oaram setMaxData can't with empty value";
}
return $this;
}
public function setHashtag($hashtag)
{
$this->_hashtag = $hashtag;
return $this;
}
/*
* Get total likes by instagramId
**/
public function getLikesByInstagramId($ig_id = "")
{
if(!empty($ig_id))
{
$this->_instagram_id = $ig_id;
$this->_get_likes = true;
}
else
{
$this->_error = true;
$this->_error_message = "getLikesByInstagramId() Can't with empty value";
}
return $this;
}
/*
* Run library start by this methode
**/
public function retrieve()
{
$this->search();
if($this->_error)
{
return $this->showErrorMessage();
}
else
{
return $this->showResults();
}
}
/*
* engine for search news by hashtag
**/
private function search($url = '')
{
/*
* Init host and path request API
**/
$host = 'api.instagram.com';
$path = '/v1/tags/'.$this->_hashtag.'/media/recent?';
// condition when get likes
if($this->_get_likes)
{
$path = '/v1/media/' . $this->_instagram_id . '?';
}
/*
* Init options for request API
**/
$query = array(
'client_id' => $this->_cliend_id,
'access_token' => $this->_access_token,
);
if(empty($url))
{
$url = "https://$host$path".http_build_query($query);
}
// execute the API and decoded the result
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
if(empty($response->data))
{
$this->_error = true;
$this->_error_message = $response;
return $this;
}
else
{
$temp_response = $response->data;
}
if($this->_get_likes)
{
$this->_result = $temp_response;
}
else
{
// merge the result
$this->_result = array_merge($this->_result, $temp_response);
}
unset($temp_response);
// check is max data is set and the result more or same than max data
// slicing the result based on the max data
if(!empty($this->_max_data) AND count($this->_result) >= $this->_max_data)
{
$this->_result = array_slice($this->_result, 0, $this->_max_data);
}
else
{
if(isset($response->pagination->next_url))
{
// do recursive
return $this->search($response->pagination->next_url);
}
}
return true;
}
/*
* For show the results
* return @array
**/
public function showResults()
{
$results = [];
$data_liked = [];
$sort_results = [];
// need for get total likes by post ig_id (instagramId)
if($this->_get_likes)
{
$returned['ig_id'] = $this->_result->id;
$returned['total_likes'] = $this->_result->likes->count;
return $returned;
}
foreach ($this->_result as $k => $v)
{
$data['ig_id'] = $v->id;
$data['created_at'] = $v->created_time;
$data['created_at_normal'] = date('d M Y H:i:s', $v->created_time);
$data['user_id'] = $v->user->id;
$data['user_name'] = $v->user->username;
$data['user_profile_picture'] = $v->user->profile_picture;
$data['user_full_name'] = $v->user->full_name;
$data['total_likes'] = $v->likes->count;
$data['instagram_url'] = $v->link;
$data['type'] = $v->type;
$data['text'] = (!empty($v->caption->text) ? $v->caption->text : "");
$data['tags'] = json_encode($v->tags);
$data['image_low_resolution_320x320'] = $v->images->low_resolution->url;
$data['image_thumbnail_150x150'] = $v->images->thumbnail->url;
$data['image_standard_resolution_640x640'] = $v->images->standard_resolution->url;
if($v->type == "video")
{
$data['video_low_resolution_480x480'] = $v->videos->low_resolution->url;
$data['video_standard_resolution_640x640'] = $v->videos->standard_resolution->url;
$data['video_low_bandwidth_480x480'] = $v->videos->low_bandwidth->url;
}
$data_liked[] = (!empty($v->likes->count) ? $v->likes->count : 0 );
$results[] = $data;
unset($data);
}
if($this->_most_liked)
{
// get index with most liked
$getIndex = array_search(max($data_liked), $data_liked);
$returned['most_liked'] = $results[$getIndex];
}
if($this->_order_by_liked)
{
if($this->_order_by_liked == 'asc' || $this->_order_by_liked == 'ASC')
{
asort($data_liked);
foreach ($data_liked as $key => $value)
{
$sort_results[] = $results[$key];
}
unset($results);
$results = $sort_results;
}
else
{
arsort($data_liked);
foreach ($data_liked as $key => $value)
{
$sort_results[] = $results[$key];
}
unset($results);
$results = $sort_results;
}
}
$returned['total'] = count($results);
$returned['items'] = $results;
$this->_result = []; // reset value;
return $returned;
}
public function showErrorMessage()
{
$this->_error = false;
return $this->_error_message;
}
}
/*
* EOF Instagram.php
**/