1

私のアプリでは、php-script を使用して少量のデータを cartodb-table に送信したいと考えています。SQL-APIを使用したいと思います。私のアプリは、次のような形式でデータを解析します。

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

編集

このソリューションのコードを使用すると、応答を出力すると、次のエラーが発生します。

{"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);
4

1 に答える 1

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);
于 2015-08-30T13:34:17.670 に答える