Skip to content

Commit 781169f

Browse files
committed
Added draft support for getting symbol lists
1 parent 57ab043 commit 781169f

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

Quandl.php

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Quandl {
1313

1414
private static $base = "https://www.quandl.com/api/v1/datasets";
1515
private static $base_multi = "https://quandl.com/api/v1/multisets";
16+
private static $base_lists = "http://www.quandl.com/api/v2/datasets";
1617

1718
// The constructor accepts an optional api_key and an
1819
// array of params. The params array may contain any key=>value
@@ -67,29 +68,18 @@ public function search($query, $per_page=null, $page=null) {
6768
return $this->getObject();
6869
}
6970

71+
//
72+
public function getList($source, $per_page=300, $page=1, $format=null) {
73+
$url = $this->getListUrl($source, $per_page, $page, $format);
74+
return $this->executeDownload($url);
75+
}
76+
7077
// getData returns data in any format for a given symbol.
7178
// Normally, you should use the getCsv, getJson or getXml
7279
// which will call getData.
73-
// This function will get the data from the cache and save
74-
// it to the cache, if a cache handler is set.
7580
public function getData($symbol=null, $format=null) {
7681
$url = $this->getUrl($symbol, $format);
77-
$this->was_cached = false;
78-
if($this->cache_handler != null) {
79-
$data = call_user_func($this->cache_handler, "get", $url);
80-
if($data) {
81-
$this->was_cached = true;
82-
}
83-
else {
84-
$data = file_get_contents($url);
85-
call_user_func($this->cache_handler, "set", $url, $data);
86-
}
87-
}
88-
else {
89-
$data = file_get_contents($url);
90-
}
91-
92-
return $data;
82+
return $this->executeDownload($url);
9383
}
9484

9585
// getUrl returns the complete URL for making a request to Quandl.
@@ -124,6 +114,44 @@ public function getUrl($symbol=null, $format=null) {
124114
return $result;
125115
}
126116

117+
// getListUrl returns a URL to the list of symbols in a
118+
// given source
119+
public function getListUrl($source, $per_page=300, $page=1, $format=null) {
120+
$format or $format = $this->default_format;
121+
$base = self::$base_lists;
122+
$params = [
123+
"query" => "*",
124+
"source_code" => $source,
125+
"per_page" => $per_page,
126+
"page" => $page,
127+
];
128+
$this->api_key and $params['auth_token'] = $this->api_key;
129+
$params = http_build_query($params);
130+
return "$base.$format?$params";
131+
}
132+
133+
// executeDownload gets a URL, and returns the downloaded document
134+
// If a cache_handler is set, it will call it to get a document
135+
// from it, and ask it to store the downloaded object where applicable.
136+
private function executeDownload($url) {
137+
$this->was_cached = false;
138+
if($this->cache_handler != null) {
139+
$data = call_user_func($this->cache_handler, "get", $url);
140+
if($data) {
141+
$this->was_cached = true;
142+
}
143+
else {
144+
$data = file_get_contents($url);
145+
call_user_func($this->cache_handler, "set", $url, $data);
146+
}
147+
}
148+
else {
149+
$data = file_get_contents($url);
150+
}
151+
152+
return $data;
153+
}
154+
127155
// convertToQuandlDate converts any time string supported by
128156
// PHP (e.g. "today-30 days") to the format needed by Quandl
129157
private static function convertToQuandlDate($time_str) {

examples.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
// UNCOMMENT ONE EXAMPLE AT A TIME
1111

1212
// Example 1: Hello World
13-
$quandl = new Quandl();
14-
$data = $quandl->getCsv($symbol);
13+
// $quandl = new Quandl();
14+
// $data = $quandl->getCsv($symbol);
1515

1616
// Example 2: API Key + JSON
1717
// $quandl = new Quandl($api_key);
@@ -57,4 +57,12 @@
5757
// $quandl = new Quandl($api_key);
5858
// $quandl->exclude_data = true;
5959
// $data = $quandl->getObject($symbol);
60+
61+
// Example 11: Symbol Lists
62+
// $quandl = new Quandl($api_key);
63+
// $data = $quandl->getList("WIKI");
64+
65+
// Example 12: Symbol Lists with Parameters
66+
$quandl = new Quandl($api_key);
67+
$data = $quandl->getList("WIKI", 100, 2, "json");
6068
?>

0 commit comments

Comments
 (0)