在开发网站的时候用到天气查询,由于是基于Wordpress的 所以有很多限制,先建一个【weather.php】的文件,然后看代码:

<?php
//获取天气
$url = 'http://m.weather.com.cn/data/';
$id = '101181101'; //焦作的代号
$data = file_get_contents($url . $id .'.html'); $obj=json_decode($data);
echo $obj->weatherinfo->city.':'.$obj->weatherinfo->weather1.' '.$obj->weatherinfo->temp1;

对于:

$url = 'http://m.weather.com.cn/data/';
$id = '101181101'; //焦作的代号
$data = file_get_contents($url . $id .'.html');

可简写为:

$data = file_get_contents('http://m.weather.com.cn/data/101181101.html');

而对于:

$obj=json_decode($data);

它是把获取的json数据转化为一个对象,方便调用;

那么最后一句:

echo $obj->weatherinfo->city.':'.$obj->weatherinfo->weather1.'  '.$obj->weatherinfo->temp1;

就是获取指定的数据并按照一定格式输出,

$obj->weatherinfo->city //城市
$obj->weatherinfo->weather1 //今天的天气
$obj->weatherinfo->temp1 //今天的气温

其他参数可以自行访问 http://m.weather.com.cn/data/101181101.html 查看

最后 在需要显示的地方

<?php include 'weather.php' ?>

即可。

最新文章

  1. Oracle中varchar,varchar2,nvarchar,nvarchar2的区别及其它数据类型描述
  2. mybatis如何做分页处理
  3. Hive函数大全
  4. Linux主机共享目录给Windows主机的方法
  5. iOS安全—阻止tweak注入hook api
  6. zoj 3229 Shoot the Bullet(无源汇上下界最大流)
  7. wpf集成unity
  8. 【程序员小助手】Synergy,感受穿越屏幕之美
  9. 遍历Javascript数组的一种方法!
  10. 国内不能使用Google解决方案(不断更新与递增中...)
  11. Docker(十):Docker安全
  12. MFC的两个问题
  13. 使用Ncat反弹Shell
  14. 10个JavaScript常见BUG及修复方法
  15. struts2 对EL的改变
  16. Luogu P2827 蚯蚓
  17. dp 优化 F2. Pictures with Kittens (hard version)
  18. mongodb 最佳可视化工具mongobooster
  19. golang优先队列
  20. AWS系列-创建AMI

热门文章

  1. P2P系统,一致性哈希和DHT
  2. Hive-RCFile文件存储格式
  3. 14 ListFragment 代码案例
  4. [ExtJS5学习笔记]第二十四节 Extjs5中表格gridpanel或者表单数据后台传输remoteFilter设置
  5. EBS系统克隆
  6. linux中echo的用法
  7. 【一天一道LeetCode】#371. Sum of Two Integers
  8. 还在繁琐的敲MVP接口和实现类吗,教你一秒搞定。
  9. Arquillian Exception:java.lang.NoClassDefFoundError
  10. [sersync+rsync] centos6.5 远程文件同步部署记录