-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGraph.php
More file actions
235 lines (200 loc) · 5.89 KB
/
Graph.php
File metadata and controls
235 lines (200 loc) · 5.89 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php //-->
/*
* This file is part of the Eden package.
* (c) 2013-2014 Openovate Labs
*
* Copyright and license information can be found at LICENSE.txt
* distributed with this package.st.
*/
namespace Eden\Facebook;
use Eden\Curl\Base as Curl;
use Eden\Facebook\Graph\Base as GraphBase;
/**
* Facebook Graph API
*
* @vendor Eden
* @package Facebook
* @author Ian Mark Muninio <ianmuninio@openovate.com>
*/
class Graph extends Base
{
const INSTANCE = 0; // sets to multiton
const GRAPH_URL = 'https://graph.facebook.com/';
protected $token = null;
/**
* Preloads the token.
*
* @param string $token
* @return void
*/
public function __construct($token)
{
Argument::i()->test(1, 'string');
$this->token = $token;
}
/**
* Returns the facebook object.
*
* @param string $name name of the facebook object
* @param scalar $args the constructor arguments
* @return \Eden\Facebook\Graph\Base
*/
public function __call($name, $args)
{
Argument::i()->test(1, 'string');
return GraphBase::i($this->token)
->__call($name, $args);
}
/**
* Deletes an object based on id.
*
* @param string $id id of the object
* @param string|null $connection [optional] the connection
* @return array
*/
public function delete($id, $connection = null)
{
Argument::i()
->test(1, 'string')
->test(2, 'string', 'null');
$url = self::GRAPH_URL . '/' . $id;
if ($connection) {
$url .= '/' . $connection;
}
$url .= '?access_token=' . $this->token;
return $this->getResponse($url, array(), Curl::DELETE);
}
/**
* Returns specific fields of an object.
*
* @param string|int $id [optional]
* @param string|array $fields
* @return array
*/
public function getFields($id = 'me', $fields = array())
{
Argument::i()
->test(1, 'string', 'int')
->test(2, 'string', 'array');
// if fields is an array
if (is_array($fields)) {
//make it into a string
$fields = implode(',', $fields);
}
// call it
return $this->getObject($id, null, array('fields' => $fields));
}
/**
* Returns the detail of any object.
*
* @param string|int $id [optional] (defaul: me) id of the object
* @param string|null $connection [optional] the page name
* @param array $query [optional] the query
* @param bool $auth [optional] (default: true) required auth
* @return array json object
*/
public function getObject($id = 'me', $connection = null, array $query = array(), $auth = true)
{
Argument::i()
->test(1, 'string', 'int')
->test(2, 'string', 'null')
->test(3, 'array')
->test(4, 'bool');
// if we have a connection
if ($connection) {
//prepend a slash
$connection = '/' . $connection;
}
// for the url
$url = self::GRAPH_URL . $id . $connection;
// if this requires authentication
if ($auth) {
// add the token
$query['access_token'] = $this->token;
}
// if we have a query
if (!empty($query)) {
//append it to the url
$url .= '?' . http_build_query($query);
}
// call it
$object = $this->getResponse($url, array());
return $object;
}
/**
* Search over all public objects in the social graph
*
* @param string
* @param string
* @param boolean
* @return array
**/
public function search($query, $type, $auth = true)
{
Argument::i()
->test(1, 'string')
->test(2, 'string')
->test(3, 'bool');
// fix query
$query = array(
'q' => $query,
'type' => $type);
// fix url, append the search word
$url = self::GRAPH_URL.'/search';
// if this requires authentication
if ($auth) {
// add the token
$query['access_token'] = $this->token;
}
// if we have a query
if (!empty($query)) {
//append it to the url
$url .= '?' . http_build_query($query);
}
// call it
$object = $this->getResponse($url, array());
return $object;
}
/**
* Get response using curl.
*
* @param string $url graph url
* @param array $post post fields
* @param string $request the request method
* @return array
*/
protected function getResponse($url, array $post = array(), $request = Curl::GET)
{
Argument::i()
->test(1, 'string')
->test(2, 'array')
->test(3, 'string');
//send it off
$curl = Curl::i()
->setUrl($url)
->setConnectTimeout(10)
->setFollowLocation(true)
->setTimeout(60)
->verifyPeer(false)
->setUserAgent(Auth::USER_AGENT)
->setHeaders('Expect');
switch ($request) {
case Curl::PUT:
$curl->setCustomPut();
break;
case Curl::GET:
$curl->setCustomGet();
break;
case Curl::DELETE:
$curl->setCustomDelete();
break;
case Curl::POST:
$curl->setPost(true)
->setPostFields(http_build_query($post));
break;
default:
}
$response = $curl->getJsonResponse();
return $response;
}
}