-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
api.php
227 lines (188 loc) · 7.83 KB
/
api.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
<?php
/*
Developer - Atul Singh
Github - https://github.com/iamatulsingh
Telegram - https://t.me/developeratul
@license Code and contributions have 'MIT License'
More details: LICENSE
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software with proper credit to original author.
*/
error_reporting(~E_NOTICE);
if ($_GET['u'] != "") {
$username = $_GET['u'];
$insta = new InstaData();
// Get details from created class
$userDetails = $insta->getUserDetails($username);
$accountDetails = $insta->getAccountDetails($username);
$timeLine = $insta->getTimeLine($username);
// $tagLikes = $insta->getTagLikes($username);
// create single json array with all data
error_reporting(~E_WARNING);
$instaData = array("userDetails"=>$userDetails, "accountDetails"=>$accountDetails, "timeLineData"=>$timeLine,
"hashTagLikes"=>$tagLikes, "tagData"=>$tagData);
echo json_encode($instaData, JSON_PRETTY_PRINT);
}
if($_GET['hashtag'] != ""){
$insta = new InstaData();
$hastag = $_GET['hashtag'];
$tagData = $insta->getTagData($hastag);
// echo $tagData;
echo json_encode($tagData, JSON_PRETTY_PRINT);
}
else{
echo '';
}
class InstaData{
public function getData($username){
$options = array('http' => array('user_agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
)
);
$context = stream_context_create($options);
error_reporting(~E_WARNING);
if(($instaLink = file_get_contents('https://www.instagram.com/' . $username, false, $context)) == false){
echo "Error: Link not found, user-id is invalid";
exit;
}
return $instaLink;
}
public function fetchUserDetails($username){
$instaLink = $this->getData($username);
$instaIDPattern = '/window._sharedData = (.*)/';
if (!preg_match($instaIDPattern, $instaLink, $matches)) {
exit;
}
$trim_data = substr($matches[1], 0, -10);
$json_output = json_decode($trim_data,true);
$json_output = $json_output['entry_data']['ProfilePage']['0']['graphql']['user'];
return $json_output;
}
public function fetchAccountDetails($username){
$details = "";
$instaLink = $this->getData($username);
$detailsPattern = '/meta content=(.\d+)(.*)/';
if (preg_match($detailsPattern, $instaLink, $res)) {
if (strpos($res[0], 'Followers') !== false) {
$details = $res[1]. "" .$res[2];
}
}
$input_line = substr($details, 1, -23);
$userDetails = preg_split("/, /", $input_line);
return $userDetails;
}
public function getTimeLine($username){
$instaLink = $this->getData($username);
$instaIDPattern = '/window._sharedData = (.*)/';
if (!preg_match($instaIDPattern, $instaLink, $matches)) {
exit;
}
$trim_data = substr($matches[1], 0, -10);
$json_output = json_decode($trim_data,true);
$json_output = $json_output['entry_data']['ProfilePage']['0']['graphql']['user']['edge_owner_to_timeline_media']['edges'];
$count = count($json_output);
$timeLine = Array();
for($i=0;$i<$count;$i++){
error_reporting(~E_NOTICE);
$post_txt = $json_output[$i]['node']['edge_media_to_caption']['edges'] ? $json_output[$i]['node']['edge_media_to_caption']['edges']['0']['node']['text'] : "";
$post_img = $json_output[$i]['node']['display_url'];
$post_likes = $json_output[$i]['node']['edge_liked_by']['count'];
$post_comments = $json_output[$i]['node']['edge_media_to_comment']['count'];
$post_time = $json_output[$i]['node']['taken_at_timestamp'];
$date = new DateTime("@$post_time");
$timeLine[$i]['post_img'] = $post_img;
$timeLine[$i]['post_txt'] = $post_txt;
$timeLine[$i]['post_time'] = $date->format('Y-m-d H:i:s');
$timeLine[$i]['post_likes'] = $post_likes;
$timeLine[$i]['post_comments'] = $post_comments;
}
return Array('data'=>$timeLine,'count'=>$count);
}
public function getUserDetails($username){
$json_output = $this->fetchUserDetails($username);
$userData = array();
$userData['img'] = $json_output['profile_pic_url_hd'];
$userData['full_name'] = $json_output['full_name'];
$userData['username'] = $json_output['username'];
$userData['is_verified'] = "false";
if($json_output['is_verified'])
$userData['is_verified'] = "true";
else
$userData['is_verified'] = "false";
$userData['id'] = $json_output['id'];
$userData['instaUrl'] = "https://instagram.com/".$json_output['username'];
// $json_userData = json_encode($userData);
// return $json_userData;
return $userData;
}
public function getAccountDetails($username){
$userDetails = $this->fetchAccountDetails($username);
$accountData = array();
$accountData['followers'] = $userDetails[0];
$accountData['follow'] = $userDetails[1];
$temp = preg_split("/ -/", $userDetails[2]);
$accountData['posts'] = $temp[0];
// $json_accountData = json_encode($accountData);
// return $json_accountData;
return $accountData;
}
// New code added here
public function getHashTageData($hashtag){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://i.instagram.com/api/v1/tags/logged_out_web_info/?tag_name=' . $hashtag);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Authority: i.instagram.com';
$headers[] = 'Accept: */*';
$headers[] = 'Accept-Language: en-US,en;q=0.9';
$headers[] = 'Cookie: csrftoken=AtXwOU4HakeAhVPX5ymMG5NerRCINGvg; mid=YuqMTwAEAAFwCGMwsQpx3HuH-L1n; ig_did=2A6AF10E-74DE-4398-A1E6-5AD079D49602; dpr=2; datr=Y4zqYg2zy9EU5ozONcNPvGYL';
$headers[] = 'Origin: https://www.instagram.com';
$headers[] = 'Referer: https://www.instagram.com/';
$headers[] = 'Sec-Ch-Ua: \".Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"';
$headers[] = 'Sec-Ch-Ua-Mobile: ?0';
$headers[] = 'Sec-Ch-Ua-Platform: \"Linux\"';
$headers[] = 'Sec-Fetch-Dest: empty';
$headers[] = 'Sec-Fetch-Mode: cors';
$headers[] = 'Sec-Fetch-Site: same-site';
$headers[] = 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36';
$headers[] = 'X-Asbd-Id: 198387';
$headers[] = 'X-Csrftoken: AtXwOU4HakeAhVPX5ymMG5NerRCINGvg';
$headers[] = 'X-Ig-App-Id: 936619743392459';
$headers[] = 'X-Ig-Www-Claim: 0';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$hashtagData = curl_exec($ch);
if (curl_errno($ch)) {
echo "Error: Hash tag value is not available for " . $hashtag;
echo 'Error:' . curl_error($ch);
exit;
}
curl_close($ch);
return $hashtagData;
}
public function getTagLikes($hashtag){
$instaHashtag = $this->getHashTageData($hashtag);
$json_output = json_decode($instaHashtag,true);
$likes = $json_output['graphql']['hashtag']['edge_hashtag_to_media']['count'];
return $likes;
}
public function getTagData($hashtag){
$instaHashtag = $this->getHashTageData($hashtag);
echo $instaHashtag;
$json_output = json_decode($instaHashtag,true);
$json_output = $json_output['graphql']['hashtag']['edge_hashtag_to_media']['edges'];
$count = count($json_output);
$hashtag_data = Array();
for($i=0;$i<$count;$i++){
error_reporting(~E_NOTICE);
$txt = $json_output[$i]['node']['edge_media_to_caption']['edges']['0']['node']['text'];
$post_img = $json_output[$i]['node']['display_url'];
$hashtag_time = $json_output[$i]['node']['taken_at_timestamp'];
$date = new DateTime("@$hashtag_time");
$hashtag_data[$i]['hashtag_img'] = $post_img;
$hashtag_data[$i]['hashtag_txt'] = $txt;
$hashtag_data[$i]['hashtag_time'] = $date->format('Y-m-d H:i:s');
}
return Array('data'=>$hashtag_data,'count'=>$count);
}
}
?>