From ac0b7705472728bbf6b8443c633cc0c4d612fc4c Mon Sep 17 00:00:00 2001 From: Kartones Date: Fri, 17 Oct 2014 12:45:02 +0200 Subject: [PATCH] Changed getRecords to use SQL API --- cartodb.class.php | 17 ++++++++++++----- example.php | 6 ++++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/cartodb.class.php b/cartodb.class.php index a0d0f0b..cade735 100644 --- a/cartodb.class.php +++ b/cartodb.class.php @@ -43,6 +43,7 @@ function __construct($config) { $this->TEMP_TOKEN_FILE_PATH = sys_get_temp_dir() . '/' . $this->subdomain . '.cartodbtempkey.txt'; $this->OAUTH_URL = 'https://' . $this->subdomain . '.cartodb.com/oauth/'; $this->API_URL = 'https://' . $this->subdomain . '.cartodb.com/api/v1/'; + $this->API_URL_V2 = 'https://' . $this->subdomain . '.cartodb.com/api/v2/'; try { if (file_exists($this->TEMP_TOKEN_FILE_PATH)) { @@ -62,7 +63,8 @@ function __toString() { return "OAuthConsumer[key=$this->key, secret=$this->secret]"; } - private function request($uri, $method = 'GET', $args = array()) { + private function request($uri, $method = 'GET', $args = array(), $apiVersion = 1) { + $url = ($apiVersion == 2 ? $this->API_URL_V2 : $this->API_URL) . $uri; $url = $this->API_URL . $uri; $sig_method = new OAuthSignatureMethod_HMAC_SHA1(); $consumer = new OAuthConsumer($this->key, $this->secret, NULL); @@ -100,9 +102,14 @@ private function request($uri, $method = 'GET', $args = array()) { return $response; } - public function runSql($sql) { - $params = array('q' => $sql); - $response = $this->request('sql', 'POST', array('params' => $params)); + public function runSql($sql, $additionalParams = array()) { + $params = array_merge(array( + 'q' => $sql, + 'rows_per_page' => 40, + 'page' => 0, + ), $additionalParams); + + $response = $this->request('sql', 'POST', array('params' => $params), 2); if ($response['info']['http_code'] != 200) { throw new Exception('There was a problem with your request: ' . var_export($response['return'], true)); @@ -256,7 +263,7 @@ public function deleteRow($table, $row_id) { * - 'page' : Page index. */ public function getRecords($table, $params = array()) { - return $this->request("tables/$table/records", 'GET', array('params' => $params)); + return $this->runSql("SELECT * FROM $table", $params); } private function getAccessToken() { diff --git a/example.php b/example.php index 4b2004b..fd1b574 100644 --- a/example.php +++ b/example.php @@ -9,7 +9,7 @@ // Check if the $key and $secret work fine and you are authorized if (!$cartodb->authorized) { error_log("uauth"); - print 'There is a problem authenticating, check the key and secret.'; + print 'There is a problem authenticating, check the key and secret.\n'; exit(); } @@ -54,7 +54,9 @@ $response = $cartodb->deleteRow($tableName, $row->id); print_r($response); -$response = $cartodb->getRecords($tableName, array('rows_per_page' => 0)); + +$response = $cartodb->getRecords($tableName, array('rows_per_page' => null)); +print_r($response); $total_rows = $response['return']['total_rows']; $response = $cartodb->getRecords($tableName, array('rows_per_page' => $total_rows)); print_r($response);