Forum Moderators: coopster

How to block echo if weather API is not responsive?

         

toplisek

11:15 am on Jan 21, 2026 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have PHP code where it is set API URL and API key. An example:
<?php
$apiKey = 'XXX';
$url = "https://api.openweathermap.org/data/3.0/onecall?lat=$lat&lon=$lon&units=metric&appid=$apiKey";
$response = file_get_contents($url);
$data = json_decode($response, true);
echo "
echo "
";
?>
If there is empty value for an API key or data feed not working, I should prevent seeing echo content. Is there possibility to block further visibility?

markRg

12:43 am on Feb 24, 2026 (gmt 0)

Top Contributors Of The Month



Perhaps you have already resolved this issue and it is no longer relevant, but I will respond anyway.
You need to check the length of the response from the API, and the length of the response will be more important in terms of data retrieval.

<?php
$apiKey = 'XXX';
$url = "https://api.openweathermap.org/data/3.0/onecall?lat=$lat&lon=$lon&units=metric&appid=$apiKey";
$response = file_get_contents($url);
if (strlen($response) > 100 )
{
echo "aaa bbb";
$data = json_decode($response, true);
echo "
echo "
";
}


?>