Skip to content

Commit 52d9386

Browse files
committed
Merge pull request #14 from DannyBen/endpoints
Add getDatabases and a generic get
2 parents 5cb8a11 + 38b20b9 commit 52d9386

File tree

4 files changed

+141
-80
lines changed

4 files changed

+141
-80
lines changed

Quandl.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,52 @@ class Quandl {
1616
public $error;
1717

1818
private static $url_templates = [
19+
"direct" => 'https://www.quandl.com/api/v3/%s.%s?%s',
1920
"symbol" => 'https://www.quandl.com/api/v3/datasets/%s.%s?%s',
2021
"search" => 'https://www.quandl.com/api/v3/datasets.%s?%s',
2122
"list" => 'https://www.quandl.com/api/v3/datasets.%s?%s',
2223
"meta" => 'https://www.quandl.com/api/v3/datasets/%s/metadata.%s',
24+
"dbs" => 'https://www.quandl.com/api/v3/databases.%s?%s',
2325
];
2426

27+
// --- API Methods
28+
2529
public function __construct($api_key=null, $format="object") {
2630
$this->api_key = $api_key;
2731
$this->format = $format;
2832
}
2933

34+
// get provides access to any Quandl API endpoint. There is no need
35+
// to include the format.
36+
public function get($path, $params=null) {
37+
$url = $this->getUrl("direct", $path, $this->getFormat(),
38+
$this->arrangeParams($params));
39+
40+
return $this->getData($url);
41+
}
42+
3043
// getSymbol returns data for a given symbol.
3144
public function getSymbol($symbol, $params=null) {
32-
$url = $this->getUrl("symbol",
33-
$symbol, $this->getFormat(),
45+
$url = $this->getUrl("symbol", $symbol, $this->getFormat(),
3446
$this->arrangeParams($params));
3547

3648
return $this->getData($url);
3749
}
3850

3951
// getMeta returns metadata for a given symbol.
40-
public function getMeta($symbol, $params=null) {
41-
$url = $this->getUrl("meta",
42-
$symbol, $this->getFormat(),
52+
public function getMeta($symbol) {
53+
$url = $this->getUrl("meta", $symbol, $this->getFormat());
54+
return $this->getData($url);
55+
}
56+
57+
// getDatabases returns the list of databases. Quandl limits it to
58+
// 100 per page at most.
59+
public function getDatabases($page=1, $per_page=100) {
60+
$params = [
61+
"per_page" => $per_page,
62+
"page" => $page,
63+
];
64+
$url = $this->getUrl("dbs", $this->getFormat(),
4365
$this->arrangeParams($params));
4466

4567
return $this->getData($url);
@@ -54,8 +76,7 @@ public function getSearch($query, $page=1, $per_page=300) {
5476
"page" => $page,
5577
"query" => $query,
5678
];
57-
$url = $this->getUrl("search",
58-
$this->getFormat(true),
79+
$url = $this->getUrl("search", $this->getFormat(true),
5980
$this->arrangeParams($params));
6081

6182
return $this->getData($url);
@@ -69,13 +90,14 @@ public function getList($source, $page=1, $per_page=300) {
6990
"per_page" => $per_page,
7091
"page" => $page,
7192
];
72-
$url = $this->getUrl("list",
73-
$this->getFormat(),
93+
$url = $this->getUrl("list", $this->getFormat(),
7494
$this->arrangeParams($params));
7595

7696
return $this->getData($url);
7797
}
7898

99+
// --- Private Methods
100+
79101
// getFormat returns one of the three formats supported by Quandl.
80102
// It is here for two reasons:
81103
// 1) we also allow "object" format. this will be sent to Quandl

README.md

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ PHP Quandl
55
[![Build Status](https://img.shields.io/travis/DannyBen/php-quandl.svg?maxAge=2592000&style=flat-square)](https://travis-ci.org/DannyBen/php-quandl)
66
[![Code Climate](https://img.shields.io/codeclimate/github/DannyBen/php-quandl.svg?maxAge=2592000&style=flat-square)](https://codeclimate.com/github/DannyBen/php-quandl)
77

8-
This library provides easy access to the
9-
[Quandl API](https://www.quandl.com/help/api)
10-
using PHP.
8+
---
119

10+
This library provides easy access to the [Quandl API][1] using PHP.
11+
12+
It provides several convenience methods to common Quandl API endpoints, as
13+
well as a generic method to access any of Quandl's endpoints directly.
14+
15+
16+
---
1217

1318
Geting Started
1419
--------------
@@ -66,6 +71,13 @@ $data = $quandl->getSearch("crude oil");
6671
$data = $quandl->getList("WIKI", 1, 10);
6772
```
6873

74+
To access any Quandl API endpoint directly, use the `get` method
75+
76+
```php
77+
$quandl = new Quandl($api_key);
78+
$data = $quandl->get("databases/WIKI");
79+
```
80+
6981
More examples can be found in the [examples.php](https://github.com/DannyBen/php-quandl/blob/master/examples.php) file
7082

7183
Caching
@@ -160,8 +172,8 @@ Holds the last API URL as requested from Quandl, for debugging.
160172
print $quandl->error;
161173
```
162174

163-
In case there was an error getting the data from Quandl, the request response
164-
will be `false` and this property will contain the error message.
175+
In case there was an error getting the data from Quandl, the request
176+
response will be `false` and this property will contain the error message.
165177

166178
#### `$was_cached`
167179

@@ -177,6 +189,23 @@ response came from the cache.
177189

178190
### Methods
179191

192+
#### `get`
193+
194+
```php
195+
mixed get( string $path [, array $params ] )
196+
```
197+
198+
Returns an object containing the response from any of Quandl's API
199+
endpoints. The format of the result depends on the value of
200+
`$quandl->format`.
201+
202+
The optional parameters array is an associative `key => value`
203+
array with any of the parameters supported by Quandl.
204+
205+
You do not need to pass `auth_token` in the array, it will be
206+
automatically appended.
207+
208+
180209
#### `getSymbol`
181210

182211
```php
@@ -213,13 +242,27 @@ string instead.
213242
mixed getList( string $source [, int $page, int $per_page] )
214243
```
215244

216-
Returns a list of symbols in a given source. Number of results per page is limited to 300 by default.
245+
Returns a list of symbols in a given source. Number of results per page is
246+
limited to 300 by default.
217247

218248

219249
#### `getMeta`
220250

221251
```php
222-
mixed getMeta( string $source [, array $params] )
252+
mixed getMeta( string $source )
253+
```
254+
255+
Returns metadata about a symbol.
256+
257+
258+
#### `getDatabases`
259+
260+
```php
261+
mixed getDatabases( [int $page, int $per_page] )
223262
```
224263

225-
Returns a metadata about a symbol.
264+
Returns a list of available databases. Number of results per page is
265+
limited to 100 by default.
266+
267+
268+
[1]: https://www.quandl.com/help/api

examples.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
//--------------------------------------------------------------
55
require_once "Quandl.php";
66

7-
$api_key = "YOUR_KEY_HERE";
7+
$api_key = $_SERVER['QUANDL_KEY'] ?: "YOUR_KEY_HERE";
88
$symbol = "GOOG/NASDAQ_AAPL";
99

1010
// Modify this call to any `exampleN` to check different samples
11-
$data = example1($api_key, $symbol);
11+
$data = example10($api_key, $symbol);
1212
print_r($data);
1313

1414
// Example 1: Hello Quandl
@@ -71,8 +71,20 @@ function example8($api_key, $symbol) {
7171
return $quandl->getMeta($symbol);
7272
}
7373

74-
// Example 9: Error Handling
75-
function example9($api_key, $symbol) {
74+
// Example 9: List of Databases
75+
function example9($api_key, $symbol=null) {
76+
$quandl = new Quandl($api_key);
77+
return $quandl->getDatabases();
78+
}
79+
80+
// Example 10: Direct Call (access any Quandl endpoint)
81+
function example10($api_key, $symbol=null) {
82+
$quandl = new Quandl($api_key);
83+
return $quandl->get('databases/WIKI');
84+
}
85+
86+
// Example 11: Error Handling
87+
function example11($api_key, $symbol) {
7688
$quandl = new Quandl($api_key, "csv");
7789
$result = $quandl->getSymbol("DEBUG/INVALID");
7890
if($quandl->error and !$result)

tests/QuandlTest.php

Lines changed: 43 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -22,101 +22,68 @@ protected function tearDown() {
2222
$this->cache_file and unlink($this->cache_file);
2323
}
2424

25+
public function testGet() {
26+
$quandl = new Quandl($this->api_key);
27+
$r = $quandl->get("datasets/GOOG/NASDAQ_AAPL", ['rows' => 5]);
28+
29+
$this->assertEquals('GOOG', $r->dataset->database_code, "TEST get database_code");
30+
$this->assertEquals(5, count($r->dataset->data), "TEST get data count");
31+
}
32+
2533
public function testCsv() {
2634
$this->_testGetSymbol("csv", 2800);
27-
$this->_testGetSymbol("csv", 2800, true);
2835
}
2936

3037
public function testXml() {
3138
$this->_testGetSymbol("xml", 14000);
32-
$this->_testGetSymbol("xml", 14000, true);
3339
}
3440

3541
public function testJson() {
3642
$this->_testGetSymbol("json", 4200);
37-
$this->_testGetSymbol("json", 4200, true);
3843
}
3944

4045
public function testObject() {
4146
$this->_testGetSymbol("object", 7400);
42-
$this->_testGetSymbol("object", 7400, true);
43-
}
44-
45-
public function testInvalidUrl() {
46-
$this->_testInvalidUrl();
47-
$this->_testInvalidUrl(true);
48-
}
49-
50-
public function testGetList() {
51-
$this->_testGetList();
52-
$this->_testGetList(true);
53-
}
54-
55-
public function testGetSearch() {
56-
$this->_testGetSearch();
57-
$this->_testGetSearch(true);
58-
}
59-
60-
public function testGetMeta() {
61-
$this->_testGetMeta();
62-
$this->_testGetMeta(true);
6347
}
6448

65-
public function testCache() {
66-
$this->_testCache();
67-
$this->cache_file and unlink($this->cache_file);
68-
$this->_testCache(true);
69-
}
70-
71-
public function cacheHandler($action, $url, $data=null) {
72-
$cache_key = md5("quandl:$url");
73-
$cache_file = __DIR__ . "/$cache_key";
74-
75-
if($action == "get" and file_exists($cache_file))
76-
return file_get_contents($cache_file);
77-
else if($action == "set")
78-
file_put_contents($cache_file, $data);
79-
80-
$this->cache_file = $cache_file;
81-
82-
return false;
49+
public function testCurl() {
50+
$this->_testGetSymbol("csv", 2800, true);
8351
}
8452

85-
private function _testInvalidUrl($force_curl=false) {
53+
public function testInvalidUrl() {
8654
$quandl = new Quandl($this->api_key, "json");
87-
$quandl->force_curl = $quandl->no_ssl_verify = $force_curl;
8855
$r = $quandl->getSymbol("INVALID/SYMBOL", $this->dates);
89-
$this->assertEquals($quandl->error, "Invalid URL",
90-
"TEST invalidUrl response");
56+
$this->assertEquals($quandl->error, "Invalid URL", "TEST invalidUrl response");
9157
}
9258

93-
private function _testGetList($force_curl=false) {
59+
public function testGetList() {
9460
$quandl = new Quandl($this->api_key);
95-
$quandl->force_curl = $quandl->no_ssl_verify = $force_curl;
9661
$r = $quandl->getList("WIKI", 1, 10);
97-
$this->assertEquals(10, count($r->datasets),
98-
"TEST getList count");
62+
$this->assertEquals(10, count($r->datasets), "TEST getList count");
9963
}
10064

101-
private function _testGetMeta($force_curl=false) {
65+
public function testGetSearch() {
66+
$quandl = new Quandl($this->api_key);
67+
$r = $quandl->getSearch("crud oil", 1, 10);
68+
$this->assertEquals(10, count($r->datasets), "TEST getSearch count");
69+
}
70+
71+
public function testGetMeta() {
10272
$quandl = new Quandl($this->api_key);
103-
$quandl->force_curl = $quandl->no_ssl_verify = $force_curl;
10473
$r = $quandl->getMeta("GOOG/NASDAQ_AAPL");
10574
$this->assertEquals('NASDAQ_AAPL', $r->dataset->dataset_code, "TEST getMeta dataset_code");
10675
$this->assertEquals('GOOG', $r->dataset->database_code, "TEST getMeta database_code");
10776
}
10877

109-
private function _testGetSearch($force_curl=false) {
78+
public function testGetDatabases() {
11079
$quandl = new Quandl($this->api_key);
111-
$quandl->force_curl = $quandl->no_ssl_verify = $force_curl;
112-
$r = $quandl->getSearch("crud oil", 1, 10);
113-
$this->assertEquals(10, count($r->datasets),
114-
"TEST getSearch count");
80+
$r = $quandl->getDatabases(1, 5);
81+
$this->assertEquals(5, count($r->databases), "TEST getDatabases count");
82+
$this->assertTrue(array_key_exists('database_code', $r->databases[0]), "TEST getDatabases keys");
11583
}
11684

117-
private function _testCache($force_curl=false) {
85+
public function testCache() {
11886
$quandl = new Quandl($this->api_key);
119-
$quandl->force_curl = $quandl->no_ssl_verify = $force_curl;
12087
$quandl->cache_handler = array($this, "cacheHandler");
12188
$r = $quandl->getSymbol($this->symbol, $this->dates);
12289
$count = count($r->dataset->data);
@@ -131,6 +98,22 @@ private function _testCache($force_curl=false) {
13198
"TEST was_cache should be true");
13299
}
133100

101+
// ---
102+
103+
public function cacheHandler($action, $url, $data=null) {
104+
$cache_key = md5("quandl:$url");
105+
$cache_file = __DIR__ . "/$cache_key";
106+
107+
if($action == "get" and file_exists($cache_file))
108+
return file_get_contents($cache_file);
109+
else if($action == "set")
110+
file_put_contents($cache_file, $data);
111+
112+
$this->cache_file = $cache_file;
113+
114+
return false;
115+
}
116+
134117
private function _testGetSymbol($format, $length, $force_curl=false) {
135118
$quandl = new Quandl($this->api_key, $format);
136119
$quandl->force_curl = $quandl->no_ssl_verify = $force_curl;
@@ -151,5 +134,6 @@ private function _testGetSymbol($format, $length, $force_curl=false) {
151134
$quandl->last_url,
152135
"TEST $format url");
153136
}
137+
154138
}
155139
?>

0 commit comments

Comments
 (0)