1

In my app I'd like to send some small amount of data to my cartodb-table with my php-script. I would like to use the SQL-API. My app parse the data in the form like:

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}

I tried several functions http_get, file_get_contents, http_request but nothing pass the data to my account. Put when I copy/paste the URL and open in the browser, everything is added.

What's the right function? There so many different ones... PHP-HTTP-Functions

EDIT

With the code from this solution I get this error, when I print out the response:

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

within the browser I get this:

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

my request code right now:

$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);
Community
  • 1
  • 1
CanadaRunner
  • 65
  • 10

1 Answers1

1

Found the result: with CURLOPT_POST => 1 it works!

$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);
CanadaRunner
  • 65
  • 10