1. < ?PHP
2. header("Content-type:text/html;
Charset=utf-8");
3. $url = "http://www.google.com/
ig/api?weather=shenzhen";
4. // 加载XML内容
5. $xml = new XMLReader();
6. $xml->open($url);
7. $condition = '';
8. $temp_c = '';
9. while ($xml->read()) {
10. // echo $xml->name, "==>",
$xml->depth, "<br>";
11. if (!empty($condition)
&& !empty($temp_c)) {
12. break;
13. }
14. if ($xml->name == 'condition'
&& empty($condition)) {
15. // 取第一个condition
16. $condition = $xml->getAttribute('data');
17. }
18. if ($xml->name == 'temp_c' &&
empty($temp_c)) {
19. // 取第一个temp_c
20. $temp_c = $xml->getAttribute('data');
21. }
22. $xml->read();
23. }
24. $xml->close();
25. echo '天气:', $condition, '< br />';
26. echo '温度:', $temp_c, '< br />';
2. header("Content-type:text/html;
Charset=utf-8");
3. $url = "http://www.google.com/
ig/api?weather=shenzhen";
4. // 加载XML内容
5. $xml = new XMLReader();
6. $xml->open($url);
7. $condition = '';
8. $temp_c = '';
9. while ($xml->read()) {
10. // echo $xml->name, "==>",
$xml->depth, "<br>";
11. if (!empty($condition)
&& !empty($temp_c)) {
12. break;
13. }
14. if ($xml->name == 'condition'
&& empty($condition)) {
15. // 取第一个condition
16. $condition = $xml->getAttribute('data');
17. }
18. if ($xml->name == 'temp_c' &&
empty($temp_c)) {
19. // 取第一个temp_c
20. $temp_c = $xml->getAttribute('data');
21. }
22. $xml->read();
23. }
24. $xml->close();
25. echo '天气:', $condition, '< br />';
26. echo '温度:', $temp_c, '< br />';
本文展示了一段使用PHP从Google天气API抓取深圳天气信息的代码实例。该脚本通过解析返回的XML数据来获取天气状况和温度,并将这些信息输出到网页上。
1268

被折叠的 条评论
为什么被折叠?



