-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-command_line.php
153 lines (134 loc) · 4.41 KB
/
index-command_line.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
<?php
require 'sdk/src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '490295574386191',
'secret' => '',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
// main code goes here----------------------------------
//database essentials
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="password"; // Mysql password
$db_name = "fb_amit";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
$db_handle = mysql_select_db("$db_name");
//database essentials done
function set($data,$key)
{
if (isset($data[$key]))
return $data[$key];
else
return NULL;
}
$keywords = array('coca-cola');
foreach( $keywords as $key){
$link = "/search?q=$key&type=post";
for($i=0;$i<2;$i++)
{
$data1=array();
$data1 = $facebook->api($link);
if(empty($data1['data']))
break;
foreach($data1 as $data)
{
foreach( $data as $number => $main_data)
{
if (!strcmp($number,'previous')){ break;}
//print_r("here");
$id = $main_data['id'];
$id_name = $main_data['from']['id'];
$name = $main_data['from']['name'];
$temp = set($main_data['from'],'category');
if($temp!=NULL)
$category = $temp;
else
$category = "USER";
$message = $main_data['message'];
$type =$main_data['type'];
$temp =$main_data['created_time'];
$created_time = substr($temp,0,10)." ".substr($temp,11,8);
$link =set($main_data,'link');
$name_link =set($main_data,'name');
$description_link =set($main_data,'description');
$picture_link =set($main_data,'picture');
$caption_link =set($main_data,'caption');
$temp = set($main_data,'shares');
if ($temp!=NULL)
$num_shares =$temp["count"];
else
$num_shares =0;
$temp = set($main_data,'likes');
if ($temp!=NULL)
$num_likes = count($temp["data"]);
else
$num_likes = 0;
//comments paging
//---adding contents to table "data"---------------
$sql = "INSERT INTO data (id_posts,keyword,name,id_name,message,type,link,name_link,caption_link,description_link,picture_link,num_shares,num_likes,category,created_time) VALUES(\"$id\",\"$key\",\"$name\",\"$id_name\",\"$message\",\"$type\",\"$link\",\"$name_link\",\"$caption_link\",\"$description_link\",\"$picture_link\",$num_shares,$num_likes,\"$category\",\"$created_time\")";
$result = mysql_query($sql);
//print_r($result);
$temp = set($main_data,'comments');
if ($temp!=NULL)
{
$temp2 = "https://graph.facebook.com/$id/comments";
do{
$data3 = array();
$index2 = strpos($temp2,"/$id");
$link2 = substr($temp2,$index2,1000);
$data3 = $facebook->api($link2);
$comments[$id] = $data3["data"];
foreach ($data3['data'] as $number=>$value)
{
$id_comments = $value['id'];
$name = $value['from']['name'];
$id_name = $value['from']['id'];
$message = $value['message'];
$temp =$value['created_time'];
$created_time = substr($temp,0,10)." ".substr($temp,11,8);
$like_count = $value['like_count'];
$sql = "INSERT INTO comments (id_posts,id_comments,keyword,name,id_name,message,created_time,like_count) VALUES(\"$id\",\"$id_comments\",\"$key\",\"$name\",\"$id_name\",\"$message\",\"$created_time\",$like_count)";
$result = mysql_query($sql);
}
$temp2 = set($data3,'next');
}while($temp2 != null);
}
//somments paging done
$j++;
}
}
$index = strpos($data1["paging"]["next"],'/search?');
$link = '';
$link = substr($data1["paging"]["next"], $index,1000);
//print_r($link);
}
}
//mysql_close($db_handle);
/*echo "<pre>";
print_r($comments);
echo "</pre>";
*/
//--------------------------------ends here---------------------------
?>