-
Notifications
You must be signed in to change notification settings - Fork 6
/
getDataFB.php
150 lines (137 loc) · 4.02 KB
/
getDataFB.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
<?php
//Code by Bcat95
include 'getImages.php';
?>
<?php
function connectData(){
//conect to mysqli
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "xinh-app";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Status: Data Connection failed: " . $conn->connect_error."<br/>");
}
return $conn;
}
?>
<form class="form-horizontal" method="post">
<button type="submit" name="Submit">Get Data Post</button>
</form>
<?php
if(isset($_POST['Submit'])) {
getJson('');
}
?>
<?php
function getJson($api){
//get 100 post / page
$soluong=25;
if ($api != ''){
$url= $api;
} else{
$url_api = "https://graph.facebook.com/v2.10/1173636692750000/feed?fields=id,full_picture,created_time,message,likes,from,place,object_id&limit=".$soluong."&access_token=";
$access_token= "";
$url= $url_api."".$access_token;
}
$response = get_web_page($url);
$resArr = array();//tao bien dang mang
$resArr = json_decode($response,true); //decode response qua mang
$data = $resArr['data'];
if ($data) {
for ($i=0 ; $i<sizeof($data); $i++){
//post($data[$i]);
updatePost($data[$i]);//update function
}
}
$paging = $resArr['paging'];
if ($paging) {
$pagingNext = $paging['next'];
getJson($pagingNext);
};
}
function post($post){
// chi luu nhung post co anh
if ($post['full_picture']){
checkPost($post);
}
}
function updatePost($post){
$conn = connectData();
$from= $post['from'];
//insert data to mysql
$id_post = $post['id'];
$user_id = $from['id'];
$user_name = $from['name'];
if (!empty($post['place'])) {
// object exists in array; do something
$place = $post['place'];
} else {
// object does not exist in array; do something else
$place = '';
}
$object_id = $post['object_id'];
$sql = "UPDATE post SET user_id='$user_id', user_name=N'$user_name', place=N'$place', object_id='$object_id'
Where (id_post = '$id_post')";
if ($conn->query($sql) === TRUE) {
echo $id_post." record update successfully</br>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
function insertPost($post){
$conn = connectData();
//insert data to mysql
$id_post = $post['id'];
$full_picture = $post['full_picture'];
$full_picture = getImages($full_picture,$id_post);
$created_time = $post['created_time'];
$message = $post['message'];
$likes = $post['likes']['count'];
$sql = "INSERT INTO post (id_post, full_picture, created_time, message, likes)
VALUES ('$id_post', '$full_picture', '$created_time', N'$message', '$likes')";
if ($conn->query($sql) === TRUE) {
echo $id_post." record created successfully</br>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
function checkPost($post){
$conn = connectData();
$id_post = $post['id'];
$sql = "SELECT `id_post` FROM post WHERE `id_post` = '$id_post'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 0) {
while($row = mysqli_fetch_assoc($result)) {
insertPost($row);
}
} else {
echo " Trung khoa chinh <br/>";
return false;
}
mysqli_close($conn);
}
function get_web_page($url) {
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_MAXREDIRS => 0, // stop after 10 redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_USERAGENT => "test", // name of client
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect
CURLOPT_TIMEOUT => 120, // time-out on response
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
?>