-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFacebookLite.class.php
125 lines (111 loc) · 4.46 KB
/
FacebookLite.class.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
<?php
/*
Class operate depended on Graph API v2.3
Last Update: 2015-08-03
Reference
https://developers.facebook.com/docs/graph-api/common-scenarios
https://developers.facebook.com/docs/graph-api/reference/v2.3/user/feed
*/
Class FacebookLite
{
public $error = '';
public function getUser($accessToken)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/me?access_token=' . $accessToken);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$fbme = json_decode($response, true);
if (!isset($fbme['id']) || !isset($fbme['verified']) and $fbme['verified'] != 1) {
$this->error = 'Facebook return invalid info with token [' . $accessToken . ']'
return false;
}
return $fbme;
}
public function getFriends($accessToken)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/me/friends?limit=100000&offset=0&access_token=' . $accessToken);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$fbFriends = json_decode($response, true);
if (empty($fbFriends['data'])) {
$this->error = 'Facebook return invalid data with token [' . $option['access_token'] . ']';
return false;
}
$friends = array();
foreach ($fbFriends['data'] as $data) {
$friends[$data['id']] = $data['name'];
}
return $friends;
}
/** You can determine whether two people are friends on Facebook, without having to parse their entire list of friends. */
public function isFriend($accessToken, $friendFbid)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/me/friends/' . $friendFbid . '?access_token=' . $accessToken);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$fbFriends = json_decode($response, true);
if (empty($fbFriends['data'])) {
return false;
}
return true;
}
public function publishLink($accessToken, $link)
{
$permissions = $this->getPermissions($accessToken);
if (empty($permissions) or !in_array('publish_actions', $permissions)) {
$this->error = 'No permission to publish feed.';
return false;
}
return $this->publishAction($accessToken, array('link' => $link));
}
public function getPermissions($accessToken)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/me/permissions?limit=100000&offset=0&access_token=' . $accessToken);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$fbPermission = json_decode($response, true);
if (empty($fbPermission['data'][0])) {
$this->error = 'Facebook return invalid data with token [' . $accessToken . ']';
return array();
}
$permission = array();
foreach ($fbPermission['data'][0] as $permissionName => $status) {
if ($status == 'granted') {
$permission[] = $permissionName;
}
}
return $permission;
}
public function publishAction($accessToken, $data)
{
if (empty($data)) {
$this->error = 'Publish data cannot be empty.';
return false;
}
$data['access_token'] = $accessToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/me/feed');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
$fbFeed = json_decode($response, true);
if (!empty($fbFeed['error']['message']) or empty($fbFeed['id'])) {
$this->error = !empty($fbFeed['error']['message']) ? $fbFeed['error']['message'] : 'Facebook return invalid data with token [' . $option['access_token'] . ']';
return false;
}
return $fbFeed['id'];
}
public function publishMessage($accessToken, $message)
{
$permissions = $this->getPermissions($accessToken);
if (empty($permissions) or !in_array('publish_actions', $permissions)) {
$this->error = 'No permission to publish feed.';
return false;
}
return $this->publishAction($accessToken, array('message' => $message));
}
}