2015-08-30 54 views
1

在我的應用程序中,我想用我的php腳本將一些少量數據發送到我的cartodb表。 我想用SQL-API。我的應用程序解析數據等的形式:CartoDB - SQL API - 使用php發佈請求

https://{account}.cartodb.com/api/v2/sql?q=INSERT INTO test_table (column_name, column_name_2, the_geom) VALUES ('this is a string', 11, ST_SetSRID(ST_Point(-110, 43),4326))&api_key={Your API key} 

我嘗試了幾種功能http_get, file_get_contents, http_request但沒有將數據傳遞到我的帳戶。當我複製/粘貼URL並在瀏覽器中打開時,所有內容都會被添加。

什麼是正確的功能?有這麼多不同的... PHP-HTTP-Functions

編輯

this solution代碼我得到這個錯誤,當我打印出來的響應:

{"error":["syntax error at end of input"]} 

瀏覽器中我得到這個範圍內:

{"rows":[],"time":0.038,"fields":{},"total_rows":1} 

我的請求代碼現在:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $fullurl); 
// Set so curl_exec returns the result instead of outputting it. 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
// Get the response and close the channel. 
$response = curl_exec($ch); 
curl_close($ch); 
print($response); 
+0

可能是因爲它要求對HTTPS證書的問題,看看這個【答案】(http://stackoverflow.com/questions/3873187/make-a-https-request-through-php-and -get-response) – dvhh

+0

作品,我得到e響應。但是我得到錯誤{「error」:[「您必須指明一個sql查詢」]} – CanadaRunner

+0

您可以編輯questiom以包含錯誤消息嗎? – dvhh

回答

1

找到結果:與CURLOPT_POST => 1它的工作原理!

$ch = curl_init(); 
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1, 
CURLOPT_URL => "https://".$cartodb_key.".cartodb.com/api/v2/sql", 
CURLOPT_USERAGENT => 'Sample cURL Request', 
CURLOPT_POST => 1, 
CURLOPT_POSTFIELDS => array(
    api_key => $api_key, 
    q => "INSERT INTO spot (".implode (", ", array_keys($data)).") VALUES (".implode (", ", $data).")" 
) 
)); 
$response = curl_exec($ch); 
curl_close($ch); 
+0

很棒,你自己找到了解決方案 – dvhh