34935-vm/api/wildfires.php
2025-10-14 18:46:14 +00:00

32 lines
1.4 KiB
PHP

<?php
// Fetches wildfire data from WFIGS and returns as JSON
header('Content-Type: application/json');
// The ArcGIS REST API endpoint for current wildfire perimeters
$url = 'https://services3.arcgis.com/T4QMspbfLg3qTGWY/arcgis/rest/services/WFIGS_Interagency_Perimeters_Current/FeatureServer/0/query?where=1=1&outFields=poly_IncidentName,poly_Acres_AutoCalc,attr_InitialResponseDateTime,attr_PercentContained&f=geojson';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); // Set to false to echo directly
curl_setopt($ch, CURLOPT_USERAGENT, 'worldsphere.ai bot'); // Some APIs require a user agent
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// This function will be called by curl for each chunk of data received.
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($curl, $data) {
echo $data;
return strlen($data);
});
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_error = curl_error($ch);
curl_close($ch);
if ($curl_error || $http_code !== 200) {
// If an error occurs, we can't send a JSON error because the headers are already sent.
// The client will likely receive a partial response and a JSON parsing error.
// This is a limitation of this streaming approach.
// Logging the error server-side would be the best approach here.
error_log("cURL Error: $curl_error, HTTP Code: $http_code");
}
?>