php獲取google當(dāng)前天氣實(shí)現(xiàn)程序
來(lái)源:程序員人生 發(fā)布時(shí)間:2014-05-12 19:28:05 閱讀次數(shù):2425次
我們會(huì)看到很多網(wǎng)站都可以實(shí)時(shí)的顯示當(dāng)時(shí)當(dāng)?shù)氐奶鞖?下面我來(lái)告訴你這種實(shí)時(shí)天氣的做吧,利用google aip接口即可實(shí)現(xiàn)獲取不同城市的天氣并顯示在自己網(wǎng)站上。
se.php,代碼如下:
- <?php
- $city = $_GET['city'];
- $data = createXml($city);
-
- $xml = simplexml_load_string($data);
- header("Content-type: text/xml");
- echo $xml->asXML();
-
-
- function createXml($city)
- {
-
- $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">請(qǐng)選擇</option>
- <option value="beijing">北京</option>
- <option value="shanghai">上海</option>
- <option value="guangzhou">廣州</option>
- <option value="wuhan">武漢</option>
- </select>
- <input type="submit" />
- </form>
- <?php
- if(!emptyempty($_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>
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)