Heim  >  Artikel  >  Backend-Entwicklung  >  php获取google当前天气实现程序_PHP教程

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

WBOY
WBOYOriginal
2016-07-13 16:56:43898Durchsuche

我们会看到很多网站都可以实时的显示当时当地的天气,下面我来告诉你这种实时天气的做吧,利用google aip接口即可实现获取不同城市的天气并显示在自己网站上。

se.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 "n{$city}n{$low}n{$high}n";
    }
    else
    {
        return "nn";
    }
}
 
// 华氏度转摄氏度
function f2c($fahrenhite)
{
    return floor(($fahrenhite - 32) / 1.8);
}

 

客户端 c.php

 

 代码如下 复制代码



天气查询






 
if(!empty($_POST['city']))
{
    $city = $_POST['city'];
    $xml = simplexml_load_file("http://127.0.0.1/rest/se.php?city={$city}");
    $html = "

City:{$xml->city}

n";
    $html .= "

Low:{$xml->low}

n";
    $html .= "

High:{$xml->high}

n";
    echo $html;
}
?>


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/631575.htmlTechArticle我们会看到很多网站都可以实时的显示当时当地的天气,下面我来告诉你这种实时天气的做吧,利用google aip接口即可实现获取不同城市的天...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn