国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

php獲取google當(dāng)前天氣實(shí)現(xiàn)程序

2024-05-04 21:56:58
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

我們會(huì)看到很多網(wǎng)站都可以實(shí)時(shí)的顯示當(dāng)時(shí)當(dāng)?shù)氐奶鞖?下面我來(lái)告訴你這種實(shí)時(shí)天氣的做吧,利用google aip接口即可實(shí)現(xiàn)獲取不同城市的天氣并顯示在自己網(wǎng)站上。

se.php,代碼如下:

  1. <?php 
  2. $city = $_GET['city']; 
  3. $data = createXml($city); 
  4.  
  5. $xml = simplexml_load_string($data); 
  6. header("Content-type: text/xml"); 
  7. echo $xml->asXML(); 
  8.  
  9. // 生成XML數(shù)據(jù) 
  10. function createXml($city
  11.     // Google 天氣API 
  12.     $weather = simplexml_load_file("http://www.google.com/ig/api?weather={$city}"); 
  13.     if(isset($weather->weather->forecast_conditions)) 
  14.     { 
  15.         $low = f2c($weather->weather->forecast_conditions->low['data']); 
  16.         $high = f2c($weather->weather->forecast_conditions->high['data']); 
  17.         return "<weather>n<city>{$city}</city>n<low>{$low}</low>n<high>{$high}</high></weather>n"
  18.     } 
  19.     else 
  20.     { 
  21.         return "<weather>n</weather>n"
  22.     } 
  23.  
  24. // 華氏度轉(zhuǎn)攝氏度 
  25. function f2c($fahrenhite
  26.     return floor(($fahrenhite - 32) / 1.8); 

客戶端 c.php,代碼如下:

  1. <html> 
  2. <head> 
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
  4. <title>天氣查詢</title> 
  5. </head> 
  6. <body> 
  7. <form method="post" action=""
  8. <select name="city"
  9. <option value="0">請(qǐng)選擇</option> 
  10. <option value="beijing">北京</option> 
  11. <option value="shanghai">上海</option> 
  12. <option value="guangzhou">廣州</option> 
  13. <option value="wuhan">武漢</option> 
  14. </select> 
  15. <input type="submit" /> 
  16. </form> 
  17. <?php 
  18. if(!emptyempty($_POST['city'])) 
  19.     $city = $_POST['city']; 
  20.     $xml = simplexml_load_file("http://127.0.0.1/rest/se.php?city={$city}"); 
  21.     $html = "<p>City:{$xml->city}</p>n"
  22.     $html .= "<p>Low:{$xml->low}</p>n"
  23.     $html .= "<p>High:{$xml->high}</p>n"
  24.     echo $html
  25. ?> 
  26. </body> 
  27. </html>  

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 承德市| 城步| 定边县| 凤翔县| 潮州市| 武宁县| 浦县| 改则县| 务川| 永修县| 麦盖提县| 万州区| 明光市| 洛南县| 门头沟区| 大关县| 偃师市| 安图县| 丹凤县| 德安县| 临清市| 阳朔县| 阳高县| 沙洋县| 普兰店市| 岳阳市| 富源县| 申扎县| 拉萨市| 巫山县| 双牌县| 邯郸市| 包头市| 买车| 湖州市| 潮州市| 那坡县| 芒康县| 宝兴县| 全南县| 洪泽县|