-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstagramapi.php
99 lines (73 loc) · 2.21 KB
/
instagramapi.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
<?php
/**
* Not documented, but returns user media up to 20 without authentication
**/
function getUserMediaNoAuth ($username='bellasjardin') {
$url = 'https://www.instagram.com/' . $username . '/media/';
//$response = file_get_contents($url);
//return $response;
return execCurl ($url);
}
/**
* User recent media requiring access token
**/
function getUserRecentMedia($username, $count) {
//get the access token
include 'accesstoken.php';
//get the user id
$json = getUserId($accesstoken);
//print $json;
$obj = json_decode($json);
$userid = $obj->data->id;
//print $userid;
//now get the user's recent media
$url = "https://api.instagram.com/v1/users/" .$userid ."/media/recent/?access_token=" .$accesstoken ."&count={$count}" ;
return execCurl ($url);
}
/**
* Get the user id for a given access token
**/
function getUserId($accesstoken) {
$url = 'https://api.instagram.com/v1/users/self/?access_token=' .$accesstoken;
return execCurl ($url);
}
/**
* Execute http curl request
**/
function execCurl($url) {
//echo "<br> URL: $url <br>";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false)
);
$curl_response = curl_exec($curl);
$curl_errno = curl_errno($curl);
if ($curl_errno > 0 || $curl_response === false ) {
$info = curl_getinfo($curl);
echo 'error occured during curl exec - ' . var_export($info) ;
echo '<br> error -----------> '. curl_error($curl);
}
curl_close($curl);
return $curl_response;
}
$function = $_GET['functionname'];
$username = $_GET['username'];
$count = $_GET['count'];
//echo ('getUserRecentMedia' == $function) ? 'true' : 'false';
//echo (strcmp($function, "getUserRecentMedia")==0) ? 'true' : 'false';
if (isset($function)) {
if (function_exists($function)){
if (strcmp($function, "getUserRecentMedia")==0 and (isset($username)) and (isset($count))) {
$response = getUserRecentMedia($username, $count);
echo $response;
}
else
if (strcmp($function, "getUserMediaNoAuth")==0 and (isset($username))) {
$response = getUserMediaNoAuth($username);
echo $response;
}
}
}
?>