用PHP获取当前城市的温度

use*_*787 0 php api time temperature

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

Jas*_*euw 12

如果您想在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;
?>
Run Code Online (Sandbox Code Playgroud)

现在$开尔文是开尔文的温度和Celcius的$ celcius!

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