/
index.php
executable file
·119 lines (88 loc) · 3.44 KB
/
index.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
<?php
$con = mysqli_connect("localhost","root","","dbName");
if (mysqli_connect_errno())
{
die "Error connecting to MySQL: " . mysqli_connect_error();
}
$fb_page = '******'; //your page name
$access_token = '******************************************************'; //your access token
function Curl_Req($url){
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
curl_close($curl);
$details = json_decode($result,true);
return $details['data'];
}
function sendPrivateRep($url, $message) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$json = '{
message: "'.$message.'"
}';
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
curl_exec($curl);
curl_close($curl);
}
function randCouponGen($con) {
$result = mysqli_query($con,"");
$arr_a = range('A', 'Z');
$arr_b = range('A', 'Z');
$arr_c = range('A', 'Z');
}
$posts = Curl_Req('https://graph.facebook.com/v2.11/'.$fb_page.'/posts?access_token='.$access_token);
foreach ($posts as $post)
{
$post_value = mysqli_real_escape_string($con, $post['id']);
$post_name = mysqli_real_escape_string($con, $post['name']);
//check DB for Post_ID
$if_post_exist = mysqli_query($con,"SELECT * FROM `dbName` WHERE `Post_ID`='$post_value'");
if(mysql_num_rows($if_post_exist) > 0) {//post exist
//get latest comments from MySQL
$comments = Curl_Req('https://graph.facebook.com/v2.11/'.$post['id'].'/comments?access_token='.$access_token);
$comments = array_filter($comments);
if (!empty($comments)){
//get old comments from MySQL
$old_comment_list = mysqli_query($con,"SELECT `Comment_List` FROM `dbName`");
if(mysql_num_rows($old_comment_list) > 0) {
$old_comment_list = explode(',', $old_comment_list);
$flag = true;
}
foreach ($comments as $comment) {
$comment_value = mysqli_real_escape_string($con, $comment['id']);
$comment_value .= ',';
if ($flag) {
if (!in_array($comment, $old_comment_list)) {
//send private_reply
sendPrivateRep('https://graph.facebook.com/v2.11/'.$comment['id'].'/private_replies?access_token='.$access_token, 'hello');
//Update comment_list
mysqli_query($con, "UPDATE `dbName` SET `Comment_List` = IFNULL(CONCAT(`Comment_List`, '$comment_value'), '$comment_value') WHERE `Post_ID` = '$post_value'");
}
}
else {
//send private_reply and Update comment_list
sendPrivateRep('https://graph.facebook.com/v2.11/'.$comment['id'].'/private_replies?access_token='.$access_token, 'hello');
mysqli_query($con, "UPDATE `dbName` SET `Comment_List` = '$comment_value' WHERE `Post_ID` = '$post_value'");
}
}
}
}
else {//post does not exist
mysqli_query($con,"INSERT INTO `dbName` (`Post_ID`,`Post_Name`) VALUES ('$post_value','$post_name')");
//loop through all the comments recieved through API
$comments = Curl_Req('https://graph.facebook.com/v2.11/'.$post['id'].'/comments?access_token='.$access_token);
$comments = array_filter($comments);
if (!empty($comments)){
foreach ($comments as $comment) {
$comment_value = mysqli_real_escape_string($con, $comment['id']);
$comment_value .= ',';
sendPrivateRep('https://graph.facebook.com/v2.11/'.$comment['id'].'/private_replies?access_token='.$access_token, 'hello');
mysqli_query($con, "UPDATE `dbName` SET `Comment_List` = '$comment_value' WHERE `Post_ID` = '$post_value'");
}
}
}
}
mysqli_close($con);
?>