在PHP中获取城市的当前温度


Get current temperature of a city in PHP

我正在开发一个需要当前城市温度的网站。如何在PHP页面中包含温度

如果希望在php应用程序中获取天气数据,则需要调用API。比如这个:http://openweathermap.org/current

如上图所示,您可以通过城市名称请求数据:在JSON中api.openweathermap.org/data/2.5/weather?q=London,uk

您可以通过类似的操作来访问这些数据:

<?php
$jsonurl = "http://api.openweathermap.org/data/2.5/weather?q=London,uk";
$json = file_get_contents($jsonurl);
$weather = json_decode($json);
$kelvin = $weather->main->temp;
$celcius = $kelvin - 273.15;
?>

现在$开尔文是开尔文的温度,$摄氏度是摄氏度!

我希望这对你有所帮助,请尽量更具体,如果这不是你的意思!