php擷取google當前天氣實現程式

來源:互聯網
上載者:User

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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.