Home  >  Article  >  Backend Development  >  PHP Get Google's Current Weather Implementation Program_PHP Tutorial

PHP Get Google's Current Weather Implementation Program_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 16:56:43932browse

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

The code is as follows
 代码如下 复制代码

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

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 "n{$city}n{$low}n{$high} n";
}
else
{
           return "nn";
}
}

// 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 = "

City:{$xml->city}

n";
    $html .= "

Low:{$xml->low}

n";
    $html .= "

High:{$xml->high}

n";
    echo $html;
}
?>


Client c.php

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631575.htmlTechArticleWe will see that many websites can display the local weather in real time. Let me tell you about this real-time Let’s do the weather, use the google aip interface to get the weather of different cities...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn