教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php获取google当前天气实现程序

php获取google当前天气实现程序

发布时间:2016-12-03   编辑:jiaochengji.com
教程集为您提供php获取google当前天气实现程序等资源,欢迎您收藏本站,我们将为您提供最新的php获取google当前天气实现程序资源
我们会看到很多网站都可以实时的显示当时当地的天气,下面我来告诉你这种实时天气的做吧,利用google aip接口即可实现获取不同城市的天气并显示在自己网站上。

se.php

<span><table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3623')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3623>

<?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);
}

 

</span>

客户端 c.php

 

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9944')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9944><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>

您可能感兴趣的文章:
Google API 获取当前天气信息的php代码
php百度天气小偷实现代码
php调用google天气api的实例代码
php 利用Google API 获取当前天气信息代码
php获取google当前天气实现程序
PHP数据采集程序采集天气网数据实例演示
PHP调用百度天气接口API实现查询实时天气
PHP百度天气预报小偷程序
php 天气预报代码一例
Google排名不稳定问题

[关闭]
~ ~