-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFacebook.php
126 lines (100 loc) · 3.2 KB
/
Facebook.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
<?php
namespace garyrutland\facebook;
use Yii;
use yii\base\Component;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Exception;
class Facebook extends Component
{
public $appId;
public $secret;
public $scope;
private $session;
private $accessToken;
public function init()
{
parent::init();
if (!Yii::$app->session->isActive) {
Yii::$app->session->open();
}
FacebookSession::setDefaultApplication($this->appId, $this->secret);
$accessToken = Yii::$app->session->get('fbAccessToken');
if ($accessToken !== null) {
$this->setSession(new FacebookSession($accessToken));
}
}
public function getSession()
{
return $this->session;
}
public function setSession(FacebookSession $session)
{
$this->setAccessToken($session->getToken());
$this->session = $session;
}
public function getAccessToken()
{
return $this->accessToken;
}
public function setAccessToken($accessToken)
{
$this->accessToken = $accessToken;
Yii::$app->session->set('fbAccesstoken', $this->accessToken);
}
public function getLoginUrl($redirectUrl, $scope = null)
{
$helper = new FacebookRedirectLoginHelper($redirectUrl);
return $helper->getLoginUrl(['scope' => $scope]);
}
public function getLoginSession($redirectUrl)
{
$helper = new FacebookRedirectLoginHelper($redirectUrl);
$this->setSession($helper->getSessionFromRedirect());
return $this->getSession();
}
public function getUser($userId = 'me')
{
try {
$request = new FacebookRequest($this->getSession(), 'GET', '/' . $userId);
return $request->execute()->getGraphObject(GraphUser::className())->asArray();
} catch (Exception $e) {}
return [];
}
public function getFriends($userId = 'me')
{
$limit = 25;
$friendCount = $this->getFriendsCount($userId);
$friends = [];
try {
for ($offset = 0; $offset <= $friendCount; $offset += $limit) {
$request = new FacebookRequest($this->getSession(), 'GET', '/' . $userId . '/friends', [
'offset' => $offset,
'limit' => $limit,
]);
$response = $request->execute()->getGraphObject()->asArray();
foreach ($response['data'] as $friend) {
array_push($friends, (array) $friend);
}
if (count($friends) < $limit) {
break;
}
}
} catch (Exception $e) {}
return $friends;
}
public function getFriendsCount($userId = 'me')
{
try {
$request = new FacebookRequest($this->getSession(), 'GET', '/' . $userId . '/friends', [
'offset' => 0,
'limit' => 0,
]);
$response = $request->execute()->getGraphObject()->asArray();
return $response['summary']->total_count;
} catch (Exception $e) {}
return 0;
}
}