-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.php
223 lines (184 loc) · 8.38 KB
/
jquery.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
<?php
include 'connection.php';
set_time_limit(0);
ini_set('default_socket_timeout',100);
session_start();
/*----------Instagram API Keys (Constants)-------*/
//ID of your client (app)
define("clientID","7b31b72433ac41ac91c6fe9915c03ac5");
//Secret password of your client
define("clientSecret","28fb8d3719434e6098567cb3c7495988");
//URL to direct the user too once logged in successfully
define("redirectURI","http://localhost:8081/GitHub/JQuery/jquery.php");
//Folder used to hold the pictures
define("imageDirectory", "Pics/");
//Function used to hold the curl permissions and config (for later use) to connect to Instagram
function connectToInstagram($url)
{
//Init starts the curl session, allowing the transfer of data from the url in the parameter
$ch = curl_init();
curl_setopt_array($ch,
array(
//Sets the array of where to talk to
CURLOPT_URL => $url,
//Return the result of 'true' if the transfer was successful
CURLOPT_RETURNTRANSFER => TRUE,
//Used to check the handshake between Instagram and your website. (Should be usually set to TRUE but for demo purpoes set to FALSE)
CURLOPT_SSL_VERIFYPEER => FALSE,
// Means to not verify a host (just a Instagram default)
CURLOPT_SSL_VERIFYHOST => 2
)); //Ends the Array
//Stores the result from executing the curl session
$result = curl_exec($ch);
//Closes the curl session
curl_close($ch);
//Returns the result of the curl session
return $result;
};//Ends the connectToInstagram function
function getMaxID($access_token,$userID)
{
$url = 'https://api.instagram.com/v1/users/'.$userID.'/media/recent/?access_token='.$access_token;
$InstagramInfo = connectToInstagram($url);
$results = json_decode($InstagramInfo, true);
$maxIDArray = (string)$results['pagination']['next_max_id'];
//echo $maxIDArray;
return (string) $maxIDArray;
}
function getComment($access_token,$mediaID,$userID)
{
$flag = TRUE;
//Variable used to display the current amount of comments on a photo (STARTING FROM 1)
$i = 1;
//Variable used to display the current amount of comments on a photo (STARTING FROM 0)
$k = 0;
//VARIABLE USED TO DISPLAY THE CURRENT PICTURE NUMBER ON
$j = 1;
//GETS THE CURRENT UNIX TIME FROM THE SERVER (DEFUNCT-ONLY USED FOR A HARD CODE TIME SET BELOW)
$time = time ();
//HARD-CODED TIME, WHICH CAME FROM THE CALLING OF THE TIME FUNCTION ABOVE. PARTICULAR CHOSEN VALUE
$time = '1418854190';
//Instagram endpoint used to get the comments and like status of a photo
$url = 'https://api.instagram.com/v1/users/'.$userID.'/media/recent/?access_token='.$access_token.'&min_timestamp='.$time;
//Function is called to iniate the connection with the Instagram API, and returns the data to $InstagramInfo
$InstagramInfo = connectToInstagram($url);
//Decodes the JSON array stored in Instagram Info and stores it into the $results variable
$results =json_decode($InstagramInfo,true);
//print_r($results);
$code = $results['meta']['code'];
//Iterates through the $userText array and assigns the values to $newText within the loop
foreach($results['data'] as $newText)
{
//ASSIGNS THE PICTURE ID FROM THE API
$pictureID = $newText['id'];
echo "The picture ID is: ", $pictureID, "</br>";
echo "Showing comments from picture #", $j,"</br>";
//VARIABLE INIT. FOR CONCATENATING THE COMMENTS TOGETHER INTO A STRING DELIMINATED BY +=
$commentCat = "+/";
//VARIABLE INIT. FOR CONCATENATING THE USER NAMES TOGETHER INTO A STRING DELIMINATED BY +=
$userCat = "+/";
//RESET THE COUNT FOR THE AMOUNT OF COMMENTS ON A PICTURE (STARTING FROM 1)
$i = 1;
//RESET THE COUNT FOR THE AMOUNT OF COMMENTS ON A PICTURE (STARTING FROM 0)
$k = 0;
//ASSIGNS THE COMMENT RETRIVED FROM THE API
$userComment = $newText['comments'];
foreach($userComment['data'] as $newText2)
{
$comment = $newText2['text'];
$commentCat .= $comment . "+/";
$user = $newText2['from']['username'];
$userCat .= $user ."+/";
echo "Comment number ", $i, " says: ", $comment, " and it is from: ",$user,"</br>";
//UPDATES THE SQL DATABASE WITH THE CONCATENATED STRING OF COMMENTS
mysql_query("UPDATE Dsp_data SET DSP_Comment = '$commentCat' WHERE DSP_PicID = $j ") or die (mysql_error());
//UPDATES THE SQL DATABASE WITH THE CONCATENATED STRING OF USER NAMES
mysql_query("UPDATE Dsp_data SET DSP_UserName = '$userCat' WHERE DSP_PicID = $j ") or die (mysql_error());
//mysql_query("UPDATE Dsp_data SET DSP_Comment = CONCAT(DSP_comment,'$comment') WHERE DSP_PicID = $j ") or die (mysql_error());
$i++;
$k++;
}//ENDS INSIDE-FOR EACH
//UPDATES THE SQL DATABASE WITH THE AMOUNT OF COMMENTS PER SPECIFIED PICTURE
mysql_query("UPDATE Dsp_data SET DSP_CommentAmt = '$k' WHERE DSP_PicID = $j ") or die (mysql_error());
//EXECUTES IF NO COMMENTS WERE LEFT ON THE PICTURE
if ($k == 0)
{
$nullValue = "No Comments";
//UPDATE SQL DATABASE WITH THE $NULLVALUE
mysql_query("UPDATE Dsp_data SET DSP_Comment = '$nullValue' WHERE DSP_PicID = $j ") or die (mysql_error());
//UPDATES THE SQL DATABASE WITH THE AMOUNT OF COMMENTS PER SPECIFIED PICTURE
mysql_query("UPDATE Dsp_data SET DSP_CommentAmt = $k WHERE DSP_PicID = $j ") or die (mysql_error());
echo "No comments were made on this photo</br>";
}//ENDS IF-STATEMENT
$j++;
}//ENDS OUTIDE-FOR EACH
}//ENDS FUNCTION
//Grabs the 'code' from the URL once redirected after user log in
if($_GET['code'])
{
//If user has already logged in, display the code below
$code=$_GET['code'];
//Authorization to access user's account
$url = "https://api.instagram.com/oauth/access_token";
//Required Instagram Permissions
$access_token_settings = array
(
'client_id' => clientID,
'client_secret' => clientSecret,
'grant_type' => 'authorization_code',
'redirect_uri' => redirectURI,
'code' => $code,
'access_token' =>'access_token'
); //Ends the Array
/*Curl allows for data tranfer from one website to another*/
//Init starts the curl session, allowing the transfer of data from the url in the parameter
$curl = curl_init($url);
//Posts to the instagram API (required by their specific API)
curl_setopt($curl, CURLOPT_POST, true);
//Allows us to set the fields of the token once posted via the access_token_settings variable above
curl_setopt($curl, CURLOPT_POSTFIELDS, $access_token_settings);
//When set to 1, all the results are returned as a string (rather than echoing everything out, thus making it easier to work with)
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//Used to check the handshake between Instagram and your website. (Should be usually set to TRUE but for demo purpoes set to FALSE)
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//Executes the start of the curl session with the session in it's parameter
$result = curl_exec($curl);
//Ends the curl session, and free up resources
curl_close($curl);
//Decodes the data recieved from curl session, and makes it readable by turning them into php variables (index's into key-value pairs)
$results =json_decode($result,true);
//Retrieves the access token from the results array
$access_token = $results['access_token'];
//echo "The access token is ",$access_token;
//Assigns the User's ID from the USER array
$userID = $results['user']['id'];
//Assigns the User's Handle from the USER array
$userHandle = $results['user']['username'];
//Assigns the User's Legal Name from the USER array
$userFullName = $results['user']['full_name'];
//Assigns the User's Picturefrom the USER array
$userPicture = $results['user']['profile_picture'];
//prints the user's ID to the screen
echo $userID,"</br>";
//prints the user's Handle to the screen
echo $userHandle,"</br>";
//prints the user's Full Name to the screen
echo $userFullName,"</br>";
//prints the user's Picture to the screen
echo('<img src=" '. $userPicture .' "/><br/>');
getComment($access_token,getMaxID($access_token,$userID),$userID);
//getMaxID($access_token,$userID);
}
else
//If the user is not logged in, Display the code below
{ ?>
<doctype html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script>
<body>
<!-- LOGIN LINK AND SCOPES -->
<a href="https://api.instagram.com/oauth/authorize/?client_id=<?php echo clientID; ?>&redirect_uri=<?php echo redirectURI; ?>&response_type=code&scope=likes+comments+relationships"> Login </a>
</body>
</html>
<?php
}
?>