1 | <?php |
||
5 | class Quandl |
||
6 | { |
||
7 | public $api_key = null; |
||
8 | public $format = null; |
||
9 | public $cache_handler = null; |
||
10 | public $was_cached = false; |
||
11 | public $force_curl = false; |
||
12 | public $no_ssl_verify = false; // disable ssl verification for curl |
||
13 | public $last_url = null; |
||
14 | public $error = null; |
||
15 | |||
16 | private static $url_templates = [ |
||
17 | "symbol" => 'https://www.quandl.com/api/v1/datasets/%s.%s?%s', |
||
18 | "search" => 'https://www.quandl.com/api/v1/datasets.%s?%s', |
||
19 | "list" => 'https://www.quandl.com/api/v2/datasets.%s?%s', |
||
20 | ]; |
||
21 | |||
22 | 8 | public function __construct($api_key = null, $format = 'object') |
|
27 | |||
28 | // getSymbol returns data for a given symbol. |
||
29 | 8 | public function getSymbol($symbol, $params = null) |
|
40 | |||
41 | // getSearch returns results for a search query. |
||
42 | // CSV output is not supported with this node so if format |
||
43 | // is set to CSV, the result will fall back to object mode. |
||
44 | public function getSearch($query, $page = 1, $per_page = 300) |
||
51 | |||
52 | // getList returns the list of symbols for a given source. |
||
53 | public function getList($query, $page = 1, $per_page = 300) |
||
61 | |||
62 | //generate the url basead in parameters |
||
63 | public function generateUrl($type = '', $format = false, $params) |
||
73 | |||
74 | // getFormat returns one of the three formats supported by Quandl. |
||
75 | // It is here for two reasons: |
||
76 | // 1) we also allow 'object' format. this will be sent to Quandl |
||
77 | // as "json" but the getData method will return a json_decoded |
||
78 | // output. |
||
79 | // 2) some Quandl nodes do not support CSV (namely search). |
||
80 | 8 | private function getFormat($omit_csv = false) |
|
88 | |||
89 | // getUrl receives a kind that points to a URL template and |
||
90 | // a variable number of parameters, which will be replaced |
||
91 | // in the template. |
||
92 | /** |
||
93 | * @param string $kind |
||
94 | */ |
||
95 | 8 | private function getUrl($kind) |
|
103 | |||
104 | // getData executes the download operation and returns the result |
||
105 | // as is, or json-decoded if 'object' type was requested. |
||
106 | /** |
||
107 | * @param string $url |
||
108 | */ |
||
109 | 8 | private function getData($url) |
|
119 | |||
120 | // executeDownload gets a URL, and returns the downloaded document |
||
121 | // either from cache (if cache_handler is set) or from Quandl. |
||
122 | 8 | private function executeDownload($url) |
|
137 | |||
138 | // attemptGetFromCache is called if a cache_handler is available. |
||
139 | // It will call the cache handler with a get request, return the |
||
140 | // document if found, and will ask it to store the downloaded |
||
141 | // object where applicable. |
||
142 | 1 | private function attemptGetFromCache($url) |
|
160 | |||
161 | // arrangeParams converts a parameters array to a query string. |
||
162 | // In addition, we add some patches: |
||
163 | // 1) trim_start and trim_end are converted from any plain |
||
164 | // language syntax to Quandl format |
||
165 | // 2) api_key is appended |
||
166 | 8 | private function arrangeParams($params) |
|
184 | |||
185 | // convertToQuandlDate converts any time string supported by |
||
186 | // PHP (e.g. "today-30 days") to the format needed by Quandl |
||
187 | 8 | private static function convertToQuandlDate($time_str) |
|
191 | |||
192 | /* |
||
193 | * download fetches url with file_get_contents or curl fallback |
||
194 | * You can force curl download by setting force_curl to true. |
||
195 | * You can disable SSL verification for curl by setting |
||
196 | * no_ssl_verify to true (solves "SSL certificate problem") |
||
197 | */ |
||
198 | 8 | private function download($url) |
|
215 | |||
216 | //check url |
||
217 | 8 | private function checkUrl($url) |
|
229 | |||
230 | //download data with file_get_contents PHP function |
||
231 | 8 | private function getDataWithFileGetContents($url) |
|
239 | |||
240 | //download data with file_get_contents PHP function |
||
241 | private function getDataWithCurl($url) |
||
266 | |||
267 | //construct a array with parameters used to query |
||
268 | private function constructParams($query, $page = 1, $per_page = 300) |
||
278 | } |
||
279 |