現在的網站都會有一些服務性的東東。如日曆,天氣預報等等!
本來想用ajax來實現的!可是ajax是不能調用遠程的xml,只能指望動態語言啦!
google天氣預報的api:http://www.google.com/ig/api?weather=$city&hl=zh-cn;
<?php
header("content-Type: text/html; charset=Utf-8");
$city = empty($_GET['city']) ? 'shaoguan' : $_GET['city'];
$content =
file_get_contents("http://www.google.com/ig/api?weather=$city&hl=zh-cn");
//$content = mb_convert_encoding($content, 'UTF-8', 'GBK'); //gbk to
utf-8
$content = iconv("GBK", "UTF-8", $content);
$xml = new DOMDocument();
$xml->loadXML($content);
$curr= $xml->getElementsByTagName('current_conditions');
$temp = $curr>getElementsByTagName("temp_c");
/**
* 要獲取Title標籤的Id屬性要分兩部走
* 1. 獲取title中所有屬性的列表也就是$title->item(0)->attributes
* 2. 獲取title中id的屬性,因爲其在第一位所以用item(0)
*
* 小提示:
* 若取屬性的值可以用item(*)->nodeValue
* 若取屬性的標籤可以用item(*)->nodeName
* 若取屬性的類型可以用item(*)->nodeType
*/
echo $temp->item(0)->attributes->item(0)->nodeValue .
"℃<br />";
?>
http://www.google.com/ig/api?weather=$city&hl=zh-cn這個接口是gbk編碼的!所以這裏用
到了一些轉碼的函數
$content = iconv(”GBK”, “UTF-8″, $content);
$content = mb_convert_encoding($content, “UTF-8″, “GBK”);
他們實現的功能都是差不多的!
更多php的幫助請查看
http://cn.php.net/manual/en/function.mb-convert-encoding.php
上面只是一個小小的例子
還要一個用simpleXML寫的例子
<?php
$city = empty($_GET['city']) ? 'zhuhai' : $_GET['city'];
$content =
file_get_contents("http://www.google.com/ig/api?weather=$city&hl=zh-cn");
$content || die("No such city's data");
$content = mb_convert_encoding($content, 'UTF-8', 'GBK');
$xml = simplexml_load_string($content);
$date =
$xml->weather->forecast_information->forecast_date->attributes();
$html = $date. "<br>\r\n";
$current = $xml->weather->current_conditions;
$condition = $current->condition->attributes();
$temp_c = $current->temp_c->attributes();
$humidity = $current->humidity->attributes();
$icon = $current->icon->attributes();
$wind=$current->wind_condition->attributes();
$icon && $icon =
$xml->weather->forecast_conditions->icon->attributes();
$html.= "當前: {$condition}, {$temp_c}°C,<img
src='http://www.google.com/ig{$icon}'/> {$humidity}{$wind}<br
/>";
foreach($xml->weather->forecast_conditions as $forecast) {
$low = $forecast->low->attributes();
$high = $forecast->high->attributes();
$icon = $forecast->icon->attributes();
$condition = $forecast->condition->attributes();
$day_of_week = $forecast->day_of_week->attributes();
$html.= "{$day_of_week} : {$high} / {$low} °C, {$condition} <img
src='http://www.google.com/ig{$icon}' /><br />";
}
header('Content-type: text/html; Charset: utf-8');
print $html;
?>
php調用google api 開發天氣預報
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
全方位深度剖析PHP7底层源码
wy53780
2020-04-23 15:39:33
Redis主從複製以及主從複製原理
LoyaltyLu
2019-02-24 23:12:38
nginx+php執行請求的工作原理
xavier
2019-02-24 15:52:41
配置Nginx支持php,出現No input file specified錯誤的解決方法
lvqingpu
2019-02-24 13:15:35
PHP7源碼編譯安裝詳解
揚那個楊
2019-02-24 12:59:32
windows下配置php
jason1982
2019-02-24 12:57:09
PHP hebrev()函數用法講解
php参考手册
2019-02-24 12:43:05
php中file_get_contents()函數用法實例
laozhang
2019-02-24 12:43:05
PHP中的pack和unpack函數
tlanyan
2019-02-24 12:35:02
<<深入PHP面向對象、模式與實踐>>讀書筆記:面向對象設計和過程式編程
tusi
2019-02-23 17:14:59
【PHP源碼分析】small內存規格的計算
LNMPR源碼研究
2019-02-23 16:42:36
源碼分析(五)—配置篇
瀟塵淵
2019-02-23 15:22:46
解決 go get 下載安裝 golang.org/x 相關包失敗的問題
Jioby
2019-02-24 13:54:53
protobuf-c的使用(一)構建
kid2412
2019-02-24 13:34:53