-
Notifications
You must be signed in to change notification settings - Fork 102
/
subscribe.php
163 lines (142 loc) · 4.36 KB
/
subscribe.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
153
154
155
156
157
158
159
160
161
162
163
<?php //-->
/*
* This file is part of the Eden package.
* (c) 2011-2012 Openovate Labs
*
* Copyright and license information can be found at LICENSE.txt
* distributed with this package.st.
*/
/**
* Facebook Subscribe
*
* @package Eden
* @category facebook
* @author Christian Symon M. Buenavista sbuenavista@openovate.com
*/
class Eden_Facebook_Subscribe extends Eden_Class {
/* Constants
-------------------------------*/
const SUBSCRIBE_URL = 'https://graph.facebook.com/%s/subscriptions';
const APPLICATION_URL = 'https://graph.facebook.com/oauth/access_token?client_id=%s&client_secret=%s&grant_type=%s';
const CREDENTIALS = 'client_credentials';
/* Public Properties
-------------------------------*/
/* Protected Properties
-------------------------------*/
protected $_token = NULL;
protected $_meta = NULL;
/* Private Properties
-------------------------------*/
/* Magic
-------------------------------*/
public static function i() {
return self::_getMultiple(__CLASS__);
}
public function __construct($clientId, $secret) {
//argument test
Eden_Facebook_Error::i()
->argument(1, 'string') //argument 1 must be a string
->argument(2, 'string'); //argument 1 must be a string
$this->_appId = $clientId;
//request a application access token
$tokenUrl = sprintf(self::APPLICATION_URL, $clientId, $secret, self::CREDENTIALS);
$appToken = file_get_contents($tokenUrl);
//convert the query to array
parse_str($appToken, $token);
//check access token is already set
if(!isset($token['access_token'])) {
//return if theres an error
return $token;
} else {
//get the access token
$this->_token = $token['access_token'];
}
}
/* Public Methods
-------------------------------*/
/**
* Returns the meta of the last call
*
* @return array
*/
public function getMeta() {
return $this->_meta;
}
/**
* Returns each of users subscribed objects and their subscribed fields
*
* @return array
*/
public function getSubscription() {
return $this->_getResponse(sprintf(self::SUBSCRIBE_URL, $this->_appId));
}
/**
* Subscribes to Facebook real-time updates
*
* @param string Type of Facebook object (user, permissions, page)
* @param string Comma-deliminated list of fields to subscribe to (e.g. "name,picture,friends,feed")
* @param url Callback-url for the real-time updates
*/
public function subscribe($object, $fields, $callbackUrl) {
//argument test
Eden_Facebook_Error::i()
->argument(1, 'string')
->argument(2, 'string')
->argument(3, 'url');
//populate fields
$query = array(
'object' => $object,
'fields' => $fields,
'callback_url' => $callbackUrl,
'verify_token' => sha1($this->_appId.$object.$callbackUrl));
//generate url
$token = array('access_token' => $this->_token);
$url = sprintf(self::SUBSCRIBE_URL, $this->_appId).'?'.http_build_query($token);
return $this->_post($url, $query);
}
/* Protected Methods
-------------------------------*/
protected function _post($url, array $query = array()) {
//set curl
$curl = Eden_Curl::i()
->setConnectTimeout(10)
->setFollowLocation(true)
->setTimeout(60)
->verifyHost(false)
->verifyPeer(false)
->setUrl($url)
->setPost(true)
->setPostFields($query)
->setHeaders('Expect');
//get response form curl
$response = $curl->getJsonResponse();
//get curl infomation
$this->_meta = $curl->getMeta();
$this->_meta['url'] = $url;
$this->_meta['query'] = $query;
$this->_meta['response'] = $response;
return $response;
}
protected function _getResponse($url, array $query = array()) {
//if needed, add access token to query
$query['access_token'] = $this->_token;
//build url query
$url = $url.'?'.http_build_query($query);
//set curl
$curl = Eden_Curl::i()
->setUrl($url)
->verifyHost(false)
->verifyPeer(false)
->setTimeout(60);
//get response from curl
$response = $curl->getJsonResponse();
//get curl infomation
$this->_meta['url'] = $url;
$this->_meta['query'] = $query;
$this->_meta['curl'] = $curl->getMeta();
$this->_meta['response'] = $response;
return $response;
}
/* Private Methods
-------------------------------*/
}