Skip to content

Commit

Permalink
add curl download fallback (fixes #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyBen committed May 19, 2015
1 parent 4cf2c11 commit f24b019
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dev.php
64 changes: 48 additions & 16 deletions Quandl.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Quandl {
public $format;
public $cache_handler = null;
public $was_cached = false;
public $force_curl = false;
public $no_ssl_verify = false; // disable ssl verification for curl
public $last_url;
public $error;

Expand Down Expand Up @@ -70,8 +72,9 @@ public function getList($source, $page=1, $per_page=300) {
// output.
// 2) some Quandl nodes do not support CSV (namely search).
private function getFormat($omit_csv=false) {
if(($this->format == "csv" and $omit_csv) or $this->format == "object")
if (($this->format == "csv" and $omit_csv) or $this->format == "object")
return "json";

return $this->format;
}

Expand All @@ -95,14 +98,11 @@ private function getData($url) {
// executeDownload gets a URL, and returns the downloaded document
// either from cache (if cache_handler is set) or from Quandl.
private function executeDownload($url) {
if($this->cache_handler == null) {
$data = @file_get_contents($url);
if(!$data)
$this->error = "Invalid URL";
}
else {
if ($this->cache_handler == null)
$data = $this->download($url);
else
$data = $this->attemptGetFromCache($url);
}

return $data;
}

Expand All @@ -113,15 +113,12 @@ private function executeDownload($url) {
private function attemptGetFromCache($url) {
$this->was_cached = false;
$data = call_user_func($this->cache_handler, "get", $url);
if($data) {
if ($data) {
$this->was_cached = true;
}
else {
$data = @file_get_contents($url);
if($data)
call_user_func($this->cache_handler, "set", $url, $data);
else
$this->error = "Invalid URL";
$data = $this->download($url);
$data and call_user_func($this->cache_handler, "set", $url, $data);
}

return $data;
Expand All @@ -134,10 +131,10 @@ private function attemptGetFromCache($url) {
// 2) api_key is appended
private function arrangeParams($params) {
$this->api_key and $params['auth_token'] = $this->api_key;
if(!$params) return $params;
if (!$params) return $params;

foreach(["trim_start", "trim_end"] as $v) {
if(isset($params[$v]) )
if (isset($params[$v]) )
$params[$v] = self::convertToQuandlDate($params[$v]);
}

Expand All @@ -149,6 +146,41 @@ private function arrangeParams($params) {
private static function convertToQuandlDate($time_str) {
return date("Y-m-d", strtotime($time_str));
}

// download fetches $url with file_get_contents or curl fallback
// You can force curl download by setting $force_curl to true.
// You can disable SSL verification for curl by setting
// $no_ssl_verify to true (solves "SSL certificate problem")
private function download($url) {
if (ini_get('allow_url_fopen') and !$this->force_curl) {
$data = @file_get_contents($url);
$data or $this->error = "Invalid URL";
return $data;
}
if (function_exists('curl_version')) {
$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$this->no_ssl_verify and curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

$data = curl_exec($curl);
$error = curl_error($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($http_code == "404") {
$data = false;
$this->error = "Invalid URL";
}
else if ($error) {
$data = false;
$this->error = $error;
}
return $data;
}
$this->error = "Enable allow_url_fopen or curl";
return false;
}
}

?>
6 changes: 3 additions & 3 deletions examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
$api_key = "YOUR_KEY_HERE";
$symbol = "GOOG/NASDAQ_AAPL";

// Uncomment and modify this call to check different samples
// $data = example3($api_key, $symbol);
// print_r($data);
// Modify this call to check different samples
$data = example1($api_key, $symbol);
print_r($data);

// Example 1: Hello Quandl
function example1($api_key, $symbol) {
Expand Down
77 changes: 54 additions & 23 deletions tests/QuandlTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php
//--------------------------------------------------------------
// Unit Tests: Quandl
// Tests: Quandl
//--------------------------------------------------------------
require_once "../Quandl.php";

class QuandlTest extends PHPUnit_Framework_TestCase {
private $api_key = "DEBUG_KEY";

private $symbol = "WIKI/AAPL";
private $symbols = ["WIKI/CSCO", "WIKI/AAPL"];
private $dates = ["trim_start" => "2014-01-01", "trim_end" => "2014-02-02"];
Expand All @@ -15,44 +16,87 @@ public function tearDown() {
}

public function testCsv() {
$this->helperGetSymbol("csv", 2800);
$this->_testGetSymbol("csv", 2800);
$this->_testGetSymbol("csv", 2800, true);
}

public function testXml() {
$this->helperGetSymbol("xml", 14000);
$this->_testGetSymbol("xml", 14000);
$this->_testGetSymbol("xml", 14000, true);
}

public function testJson() {
$this->helperGetSymbol("json", 4200);
$this->_testGetSymbol("json", 4200);
$this->_testGetSymbol("json", 4200, true);
}

public function testObject() {
$this->helperGetSymbol("object", 12000);
$this->_testGetSymbol("object", 12000);
$this->_testGetSymbol("object", 12000, true);
}

public function testInvalidUrl() {
$this->_testInvalidUrl();
$this->_testInvalidUrl(true);
}

public function testGetList() {
$this->_testGetList();
$this->_testGetList(true);
}

public function testGetSearch() {
$this->_testGetSearch();
$this->_testGetSearch(true);
}

public function testCache() {
$this->_testCache();
$this->cache_file and unlink($this->cache_file);
$this->_testCache(true);
}

public function cacheHandler($action, $url, $data=null) {
$cache_key = md5("quandl:$url");
$cache_file = __DIR__ . "/$cache_key";

if($action == "get" and file_exists($cache_file))
return file_get_contents($cache_file);
else if($action == "set")
file_put_contents($cache_file, $data);

$this->cache_file = $cache_file;

return false;
}

private function _testInvalidUrl($force_curl=false) {
$quandl = new Quandl($this->api_key, "json");
$quandl->force_curl = $quandl->no_ssl_verify = $force_curl;
$r = $quandl->getSymbol("INVALID/SYMBOL", $this->dates);
$this->assertEquals($quandl->error, "Invalid URL",
"TEST invalidUrl response");
}

public function testGetList() {
private function _testGetList($force_curl=false) {
$quandl = new Quandl($this->api_key);
$quandl->force_curl = $quandl->no_ssl_verify = $force_curl;
$r = $quandl->getList("WIKI", 1, 10);
$this->assertEquals(10, count($r->docs),
"TEST getList count");
}

public function testGetSearch() {
private function _testGetSearch($force_curl=false) {
$quandl = new Quandl($this->api_key);
$quandl->force_curl = $quandl->no_ssl_verify = $force_curl;
$r = $quandl->getSearch("crud oil", 1, 10);
$this->assertEquals(10, count($r->docs),
"TEST getSearch count");
}

public function testCache() {
private function _testCache($force_curl=false) {
$quandl = new Quandl($this->api_key);
$quandl->force_curl = $quandl->no_ssl_verify = $force_curl;
$quandl->cache_handler = array($this, "cacheHandler");
$r = $quandl->getSymbol($this->symbol, $this->dates);
$count = count($r->data);
Expand All @@ -67,22 +111,9 @@ public function testCache() {
"TEST was_cache should be true");
}

public function cacheHandler($action, $url, $data=null) {
$cache_key = md5("quandl:$url");
$cache_file = __DIR__ . "/$cache_key";

if($action == "get" and file_exists($cache_file))
return file_get_contents($cache_file);
else if($action == "set")
file_put_contents($cache_file, $data);

$this->cache_file = $cache_file;

return false;
}

private function helperGetSymbol($format, $length) {
private function _testGetSymbol($format, $length, $force_curl=false) {
$quandl = new Quandl($this->api_key, $format);
$quandl->force_curl = $quandl->no_ssl_verify = $force_curl;
$r = $quandl->getSymbol($this->symbol, $this->dates);
$quandl_format = $format;
if(is_object($r)) {
Expand Down

0 comments on commit f24b019

Please sign in to comment.