Вы должны получить ответ
{"cod":"404","message":"city not found"}
от API, если он не находит город в вашем запросе, чтобы вы могли обработать ответ что-то вроде ...
$weatherArray = json_decode($urlContents, true);
if ( isset($weatherArray['cod']) && $weatherArray['cod'] == "404" ) {
$message = "No city found";
}
else {
// Process weather data
}