php获取google当前天气实现程序
se.php
<?php
代码如下
复制代码
$city = $_GET['city'];
$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 "<weather>n<city>{$city}</city>n<low>{$low}</low>n<high>{$high}</high></weather>n";
}
else
{
return "<weather>n</weather>n";
}
}
// 华氏度转摄氏度
function f2c($fahrenhite)
{
return floor(($fahrenhite - 32) / 1.8);
}
客户端 c.php
代码如下 | 复制代码 |
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>天气查询</title> </head> <body> <form method="post" action=""> <select name="city"> <option value="0">请选择</option> <option value="beijing">北京</option> <option value="shanghai">上海</option> <option value="guangzhou">广州</option> <option value="wuhan">武汉</option> </select> <input type="submit" /> </form> <?php if(!empty($_POST['city'])) { $city = $_POST['city']; $xml = simplexml_load_file("http://127.0.0.1/rest/se.php?city={$city}"); $html = "<p>City:{$xml->city}</p>n"; $html .= "<p>Low:{$xml->low}</p>n"; $html .= "<p>High:{$xml->high}</p>n"; echo $html; } ?> </body> </html> |
文章地址:http://www.phprm.com/develop/44965.html
转载随意^^请带上本文地址!