-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyql.php
64 lines (57 loc) · 1.52 KB
/
yql.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
<?
/**
* yql class.
*
* Wrapper class for all YQL calls
*/
class yql {
/**
* YQL base url
*
* Base URL to which all YQL calls are made
*/
const yqlUrl = 'http://query.yahooapis.com/v1/public/yql';
/**
* Perform a YQL query
*
* @access public
* @param string $query
* @param array $args. (default: array())
* @param bool $diagnostics (default: false)
* @return mixed $response
* Returns the raw results of the YQL query
* Should be delegated to by other methods in child classes
*/
public function query($query, $args = array(), $diagnostics = false){
$queryUrl = self::yqlUrl."?q=" . urlencode(self::encodeQuery($query, $args))."&format=json&env=".urlencode("store://datatables.org/alltableswithkeys") . ( $diagnostics ? "&diagnostics=true" : "" );
$ch = curl_init($queryUrl);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close($ch);
if ($json !== false){
$data = json_decode($json, true);
return $data;
} else {
return false;
}
}
/**
* Generates the query with optional arguments
*
* @access public
* @param string $queryFormat
* @param array $args
* @return string
* Uses sprintf syntax to generate a query.
* Fills in placeholders using values in the $args array
*/
function encodeQuery($queryFormat, $args){
foreach($args as &$arg){
$arg = addslashes($arg);
}
$query = vsprintf($queryFormat, $args);
return $query;
}
}
?>