-
Notifications
You must be signed in to change notification settings - Fork 27
/
Factual.php
executable file
·355 lines (318 loc) · 10.6 KB
/
Factual.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
namespace Factual;
/**
* Requires PHP5, php5-curl, SPL (for autoloading)
*/
//Oauth libs (from http://code.google.com/p/oauth-php/)
require_once ('oauth-php/library/OAuthStore.php');
require_once ('oauth-php/library/OAuthRequester.php');
/**
* Represents the public Factual API. Supports running queries against Factual
* and inspecting the response. Supports the same levels of authentication
* supported by Factual's API.
* This is a refactoring of the Factual Driver by Aaron: https://github.com/Factual/factual-java-driver
* @author Tyler
* @package Factual
* @license Apache 2.0
*/
class Factual {
protected $factHome; //string assigned from config
protected $signer; //OAuthStore object
protected $config; //array from config.ini file on construct
protected $geocoder; //geocoder object (unsupported, experimental)
protected $configPath = "config.ini"; //where the config file is found: path + file
protected $lastTable = null; //last table queried
protected $fetchQueue = array (); //array of queries teed up for multi
/**
* Constructor. Creates authenticated access to Factual.
* @param string key your oauth key.
* @param string secret your oauth secret.
*/
public function __construct($key, $secret) {
//load configuration
$this->loadConfig();
$this->factHome = $this->config['factual']['endpoint']; //assign endpoint
//create authentication object
$options = array (
'consumer_key' => $key,
'consumer_secret' => $secret
);
$this->signer = \OAuthStore :: instance("2Leg", $options);
//register autoloader
spl_autoload_register(array (
get_class(),
'factualAutoload'
));
}
/**
* Sets location of config file at runtime
* @param string path path+filename
* @return void
*/
protected function setConfigPath($path) {
$this->configPath = $path;
}
/**
* Loads config file from ini
* @return void
*/
protected function loadConfig() {
if (!$this->config) {
try {
$this->config = parse_ini_file($this->configPath, true);
} catch (\Exception $e) {
throw new \Exception("Failed parsing config file");
}
}
}
/**
* Change the base URL at which to contact Factual's API. This
* may be useful if you want to talk to a test or staging
* server withou changing config
* Example value: <tt>http://staging.api.v3.factual.com/t/</tt>
* @param urlBase the base URL at which to contact Factual's API.
* @return void
*/
public function setFactHome($urlBase) {
$this->factHome = $urlBase;
}
/**
* Convenience method to return Crosswalks for the specific query.
* @param string table Table name
* @param object query Query Object
*/
public function crosswalks($table, $query) {
return $this->fetch($table, $query)->getCrosswalks();
}
/**
* Factual Fetch Abstraction
* @param string tableName The name of the table you wish to query (e.g., "places")
* @param obj query The query to run against <tt>table</tt>.
* @return object ReadResponse object with result of running <tt>query</tt> against Factual.
*/
public function fetch($tableName, $query) {
switch (get_class($query)) {
case "FactualQuery" :
$res = new ReadResponse($this->request($this->urlForFetch($tableName, $query)));
break;
case "CrosswalkQuery" :
$res = new CrosswalkResponse($this->request($this->urlForCrosswalk($tableName, $query)));
break;
case "ResolveQuery" :
$res = new ResolveResponse($this->request($this->urlForResolve($tableName, $query)));
break;
case "FacetQuery" :
$res = new ReadResponse($this->request($this->urlForFacets($tableName, $query)));
break;
default :
throw new \Exception(__METHOD__ . " class type '" . get_class($query) . "' not recognized");
$res = false;
}
$this->lastTable = $tableName; //assign table name to object for logging
return $res;
}
/**
* Build query string without running fetch
* @param string tableName The name of the table you wish to query (e.g., "places")
* @param obj query The query to run against <tt>table</tt>.
* @return string
*/
public function buildQuery($tableName, $query) {
switch (get_class($query)) {
case "FactualQuery" :
$res = $this->urlForFetch($tableName, $query);
break;
case "CrosswalkQuery" :
$res = $this->urlForCrosswalk($tableName, $query);
break;
case "ResolveQuery" :
$res = $this->urlForResolve($tableName, $query);
break;
case "FacetQuery" :
$res = $this->urlForFacets($tableName, $query);
break;
default :
throw new \Exception(__METHOD__ . " class type '" . get_class($query) . "' not recognized");
$res = false;
}
return $res;
}
/**
* Resolves and returns resolved entity or null (shortcut method -- experimental)
* @param string tableName Table name
* @param array vars Attributes of entity to be matched in key=>value pairs
* @return array | null
*/
public function resolve($tableName, $vars) {
$query = new ResolveQuery();
foreach ($vars as $key => $value) {
$query->add($key, $value);
}
$res = new ResolveResponse($this->request($this->urlForResolve($tableName, $query)));
return $res->getResolved();
}
/**
* @return object SchemaResponse object
*/
public function schema($tableName) {
return new SchemaResponse($this->request($this->urlForSchema($tableName)));
}
protected function urlForSchema($tableName) {
return $this->factHome . "t/" . $tableName . "/schema";
}
protected function urlForCrosswalk($tableName, $query) {
return $this->factHome . $tableName . "/crosswalk?" . $query->toUrlQuery();
}
protected function urlForResolve($tableName, $query) {
return $this->factHome . $tableName . "/resolve?" . $query->toUrlQuery();
}
protected function urlForFetch($tableName, $query) {
return $this->factHome . "t/" . $tableName . "?" . $query->toUrlQuery();
}
protected function urlForFacets($tableName, $query) {
return $this->factHome . "t/" . $tableName . "/facets?" . $query->toUrlQuery();
}
protected function urlForMulti() {
$homeLen = strlen($this->factHome) - 1;
foreach ($this->fetchQueue as $index => $mQuery) {
$call = rawurlencode(substr($this->buildQuery($mQuery['table'], $mQuery['query']), $homeLen));
$queryStrings[] = "\"" . $index . "\":" . "\"" . $call . "\"";
$res['response'][] = $mQuery['query']->getResponseType();
}
$res['url'] = "http://api.v3.factual.com/multi?queries={" . implode(",", $queryStrings) . "}";
return $res;
}
/*
protected function urlForGeocode() {
return "places/geocode";
}
*/
protected function urlForGeopulse($tableName, $query) {
return "places/geopulse?" . $query->toUrlQuery();
}
/**
* Queue a request for inclusion in a multi request.
* @param string table The name of the table you wish to use crosswalk against (e.g., "places")
* @param obj query Query object to run against <tt>table</tt>.
* @param string name Name of this query to help you distinguish return values
*/
public function multiQueue($table, $query, $name) {
$this->fetchQueue[$name] = array (
'query' => $query,
'table' => $table
);
return $this->fetchQueue;
}
/**
* Use this to send all queued reads as a multi request
* @return response for a multi request
*/
public function multiFetch() {
//get response types required for multi objects
$res = $this->urlForMulti();
return new MultiResponse($this->request($res['url']), $res['response']);
}
/**
* Signs a 'raw' request (a complete query) and returns the JSON results
* Note that this does not process the quey at all -- just signs and returns results
* @param string urlStr unsigned URL request. Must be correctly escaped.
* @return string JSON reponse
*/
public function rawGet($urlStr) {
$res = $this->request($urlStr);
return $res['body'];
}
/**
* Sign the request, perform a curl request and return the results
* @param string urlStr unsigned URL request
* @return array ex: array ('code'=>int, 'headers'=>array(), 'body'=>string)
*/
protected function request($urlStr) {
$requestMethod = "GET";
$params = null;
$customHeaders[CURLOPT_HTTPHEADER] = array (
"X-Factual-Lib: " . $this->config['factual']['driverversion']
); //custom header
// Build request with OAuth request params
$request = new \OAuthRequester($urlStr, $requestMethod, $params);
//Make request
$result = $request->doRequest(0, $customHeaders);
$result['request'] = $urlStr; //pass request string onto response
$result['tablename'] = $this->lastTable; //pass table name to result object (not available with rawGet())
//exception handling
if ($result['code'] >= 400) {
$body = json_decode($result['body'], true);
//get a boatload of debug data
$info['code'] = $result['code'];
$info['version'] = $body['version'];
$info['status'] = $body['status'];
$info['error_type'] = $body['error_type'];
$info['message'] = $body['message'];
$info['headers'] = $result['headers'];
$info['request'] = $result['request'];
$info['driver'] = $this->config['factual']['driverversion'];
if (!empty ($result['tablename'])) {
$info['tablename'] = $result['tablename'];
}
$info['method'] = $requestMethod;
//chuck exception
$factualE = new FactualApiException($info);
throw $factualE;
}
return $result;
}
/**
* Gets driver version
* @return string
*/
public function version() {
return $this->config['factual']['driverversion'];
}
//The following methods are included as handy convenience; unsupported and experimental
//They rely on a loosely-coupled third-party service that can be easily swapped out
/**
* Geocodes address string or placename
* @param string q
* @return array
*/
public function geocode($address) {
return $this->getGeocoder()->geocode($address);
}
/**
* Reverse geocodes long/lat to the smallest bounding WOEID
* @param real long Decimal Longitude
* @param real lat Decimal Latitude
* @return array single result
*/
public function reverseGeocode($lon, $lat) {
return $this->getGeocoder()->reversegeocode($lon, $lat);
}
/**
* Geocodes address string or placename
* @param string q
* @return array
*/
protected function getGeocoder() {
if (!$this->geocoder) {
$this->geocoder = new GeocoderWrapper;
}
return $this->geocoder;
}
/**
* Autoloader for file dependencies
* Called by spl_autoload_register() to avoid conflicts with autoload() methods from other libs
*/
public static function factualAutoload($className) {
// don't interfere with other classloaders
if (0 !== strpos($className, 'Factual\\'))
{
return;
}
$filename = str_replace('Factual\\', '', $className) . '.php';
if (!file_exists($filename)) {
return;
}
require $filename;
}
}
?>