Home > Article > Backend Development > PHP Get Google's Current Weather Implementation Program_PHP Tutorial
We will see that many websites can display the local weather in real time. Let me tell you how to do this real-time weather. You can use the Google AIP interface to obtain the weather of different cities and display it on your own website. .
se.php
$city = $_GET['city'];
City:{$xml->city} Low:{$xml->low} High:{$xml->high}
Client c.php
The code is as follows
代码如下
复制代码
$data = createXml($city);
$xml = simplexml_load_string($data);
header("Content-type: text/xml");
echo $xml->asXML();
// 生成XML数据
function createXml($city)
{
// Google 天气API
$weather = simplexml_load_file("http://www.google.com/ig/api?weather={$city}");
if(isset($weather->weather->forecast_conditions))
{
$low = f2c($weather->weather->forecast_conditions->low['data']);
$high = f2c($weather->weather->forecast_conditions->high['data']);
return "
}
else
{
return "
}
}
// 华氏度转摄氏度
function f2c($fahrenhite)
{
return floor(($fahrenhite - 32) / 1.8);
}Copy code
$city = $_GET['city'];
$data = createXml($city);
$xml = simplexml_load_string($data);
header("Content-type: text/xml");
echo $xml->asXML();
// Generate XML data
function createXml($city)
{
// Google Weather API
$weather = simplexml_load_file("http://www.google.com/ig/api?weather={$city}");
If(isset($weather->weather->forecast_conditions))
{
$low = f2c($weather->weather->forecast_conditions->low['data']);
$high = f2c($weather->weather->forecast_conditions->high['data']);
return "
}
else
{
return "
}
}
// Convert Fahrenheit to Celsius
function f2c($fahrenhite)
{
Return floor(($fahrenhite - 32) / 1.8);
}
代码如下
复制代码
if(!empty($_POST['city']))
{
$city = $_POST['city'];
$xml = simplexml_load_file("http://127.0.0.1/rest/se.php?city={$city}");
$html = "
$html .= "
$html .= "
echo $html;
}
?>