@@ -13,6 +13,7 @@ class Quandl {
13
13
14
14
private static $ base = "https://www.quandl.com/api/v1/datasets " ;
15
15
private static $ base_multi = "https://quandl.com/api/v1/multisets " ;
16
+ private static $ base_lists = "http://www.quandl.com/api/v2/datasets " ;
16
17
17
18
// The constructor accepts an optional api_key and an
18
19
// array of params. The params array may contain any key=>value
@@ -67,29 +68,18 @@ public function search($query, $per_page=null, $page=null) {
67
68
return $ this ->getObject ();
68
69
}
69
70
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
+
70
77
// getData returns data in any format for a given symbol.
71
78
// Normally, you should use the getCsv, getJson or getXml
72
79
// 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.
75
80
public function getData ($ symbol =null , $ format =null ) {
76
81
$ 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 );
93
83
}
94
84
95
85
// getUrl returns the complete URL for making a request to Quandl.
@@ -124,6 +114,44 @@ public function getUrl($symbol=null, $format=null) {
124
114
return $ result ;
125
115
}
126
116
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
+
127
155
// convertToQuandlDate converts any time string supported by
128
156
// PHP (e.g. "today-30 days") to the format needed by Quandl
129
157
private static function convertToQuandlDate ($ time_str ) {
0 commit comments