I'm trying to download a GeoJSON file using the Carto SQL API and PHP. I can download the file successfully by entering the single line SQL query in the browser or using JavaScript's window.open(), but I cant seem to figure out how to do it using PHP.
The format of the SQL API call is:
https://{username}.carto.com/api/v2/sql?format=GeoJSON&q=SELECT * FROM {table_name}
My attempt:
<?php
$query = 'https://{username}.carto.com/api/v2/sql?format=GeoJSON&filename=ligonier_south_goshen1.geojson&q=SELECT * FROM {username}.ligonier_south_goshen1';
file_get_contents($query);
?>
I've also tried:
<?php
$query = 'https://{username}.carto.com/api/v2/sql?format=GeoJSON&q=SELECT * FROM {username}.ligonier_south_goshen1';
$fileName = 'ligonier_south_goshen1.geojson';
$result = file_get_contents($query);
file_put_contents($fileName, $result);
if(file_put_contents( $fileName, $result)) {
echo "File downloaded successfully";
}
else {
echo "File downloading failed.";
}
?>
Either I'm missing something or I need to use a different method of getting that data. Any help or direction would be very much appreciated.