dedecms用webservice添加天气预报
来源:互联网 发布:java 源码保护 编辑:程序博客网 时间:2024/06/15 22:04
环境:wamp+dedecms
自己弄一个地方小网站,由于对dedecms比较熟悉就选择了它。很快就有了第一个需求——未来3天天气预报,我自己的IE主页是2345.com第一个想到的办法就是用小偷程序抓取2345.com上的天气预报内容,一看源代码,估计是为了防止他人盗用,网站上的天气小图标尽然是用的class利用样式生成出来的,天气情况有这么多种,我不可能把它的所有class样式都一下子弄出来吧。遇到问题找google,找啊找,在找的过程中发现用小偷程序,不好弄,效率不高。最后找到了专门定制天气插件的网站,http://service.weather.com.cn/plugin/index.shtml,但是这个只能显示当天的,http://www.thinkpage.cn/weather/widget.aspx这个倒是挺好的,不过弄上去后发现有广告,广告问题很快就解决了 用css把广告部分不显示即可,最后还有很无语的竟然有链接,恩又不行。
找啊找,最后同事看到我找这个于是就说了句用webservice,这正是我想要的终于找到了正确的方法。拿来数据自己呈现,自己想怎么弄就怎么弄。于是开始编码
- dede用的是生成静态页面,要调用PHP程序,用Ajax,第一步调用Ajax
<script language="javascript" type="text/javascript" src="{dede:global.cfg_cmsurl/}/include/dedeajax2.js"></script>
<script type="text/javascript">
function loadWeather() {
var targetObj = document.getElementById('weather');
var waithtml = "<div style='width:100%;align:center'><img src='{dede:global.cfg_cmsurl/}/images/loadinglit.gif' />加载中...</div>";
var myajax = new DedeAjax(targetObj, true, true, '', 'x', waithtml);
myajax.SendGet2("{dede:global.cfg_templets_dir /}/widget/weather_ajax.php");
DedeXHTTP = null;
}
loadWeather();
</script> - 第二步写后台代码weather_ajax.php
<?php
require_once('../../include/common.inc.php');
soap_weather();
function soap_weather() {
$weatherFile = DEDETEMPLATE.'/default/weather.htm';
$numFile = DEDEROOT.'/num.txt';
$lifetime = 3600;
if(!checkWeatherCache($lifetime,$weatherFile)) {
$soap = new SoapClient('http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl');
$param['theCityName'] = '长沙';
$res = $soap->getWeatherbyCityName($param);
$now = $res->getWeatherbyCityNameResult->string[10];
$now_msg = $res->getWeatherbyCityNameResult->string[6];
$now_msg = substr($now_msg,strpos($now_msg,' '));
$today_tep = $res->getWeatherbyCityNameResult->string[5];
$tomor_tep = $res->getWeatherbyCityNameResult->string[12];
$atomor_tep = $res->getWeatherbyCityNameResult->string[17];
$now_pic1 = $res->getWeatherbyCityNameResult->string[8];
$now_pic2 = $res->getWeatherbyCityNameResult->string[9];
$tomor_pic1 = $res->getWeatherbyCityNameResult->string[15];
$tomor_pic2 = $res->getWeatherbyCityNameResult->string[16];
$atomor_pic1 = $res->getWeatherbyCityNameResult->string[20];
$atomor_pic2 = $res->getWeatherbyCityNameResult->string[21];
$updateTime = $res->getWeatherbyCityNameResult->string[4];
$now = explode(";",$now);
$now1 = explode(":",$now[0]);
$now_tep = (float)$now1[2];
$now1 = explode(":",$now[2]);
$now_shidu = $now1[1];
$now1 = explode(":",$now[5]);
$now_zwx = $now1[1];
$content = '
<div class="weather w_right">
<ul>
<li><strong>后天</strong></li>
<li><img src="/images/weather/'.$atomor_pic1.'"/><img src="/images/weather/'.$atomor_pic2.'"/></li>
<li>'.$atomor_tep.'</li>
<ul>
</div>
<div class="weather w_right">
<ul>
<li><strong>明天</strong></li>
<li><img src="/images/weather/'.$tomor_pic1.'"/><img src="/images/weather/'.$tomor_pic2.'"/></li>
<li>'.$tomor_tep.'</li>
<ul>
</div>
<div class="weather w_right">
<ul>
<li><strong>今天</strong></li>
<li><img src="/images/weather/'.$now_pic1.'"/><img src="/images/weather/'.$now_pic2.'"/></li>
<li>'.$today_tep.'</li>
<ul>
</div>
<div class="weather w_middle">
<ul>
<li style="display:none">'.$updateTime.'</li>
<li>气温:'.$now_tep.'</li>
<li class="m">紫外线:'.$now_zwx.'</li>
<li>湿度:'.$now_shidu.'</li>
<ul>
</div>
<div class="weather w_left">
<ul>
<li><strong>长沙</strong></li>
<li><img src="/images/weather/'.$now_pic1.'"/><img src="/images/weather/'.$now_pic2.'"/></li>
<li>'.$now_msg.'</li>
<ul>
</div>';
file_put_contents($weatherFile,$content);
$count = file_get_contents($numFile);
$count = $count + 1;
file_put_contents($numFile,$count);
echo $content;exit;
}
$content = file_get_contents($weatherFile);
echo $content;
}
function checkWeatherCache($lifetime,$weatherFile) {
if(!file_exists($weatherFile)) {
return false;
}
if((filemtime($weatherFile)+$lifetime)<time()) {
return false;
}
return true;
}
?> - 完成OK,这里用到了简单的文件缓存技术,在调用后台程序时,程序将检测我获取到的数据保存文件是否超过了一个小时的有效时间filemtime($weatherFile)+$lifetime)<time(),超过则调用webservice,否则直接调用数据保存文件,这样就能够提高效率
- dedecms用webservice添加天气预报
- C#添加WebService服务实现天气预报
- [WEBSERVICE]天气预报
- 天气预报webservice
- webservice天气预报
- C#添加WebService服务实现天气预报(查询)
- 一个好用的天气预报WebService
- 用C# 调用webservice(天气预报)
- 用webservice发布自己的天气预报服务
- 添加天气预报
- 免费好用稳定的天气预报WebService - Weather WebService
- 免费好用稳定的天气预报WebService - Weather WebService
- 天气预报webservice代码
- 天气预报webservice代码
- 天气预报 WebService 简单例子
- C# 天气预报WebService
- webservice简单应用(天气预报)
- 使用WebService实现天气预报
- C#中Get,Set的详细说明
- Android如何解析Intent
- 启动tomcat的时候报Exception starting filter struts2的解决办法
- 利用J2ME开发移动3D游戏之3D图形API
- Port and CMF
- dedecms用webservice添加天气预报
- 我是IT界的一只小小鸟,天空是我的极限!
- Ogre-天龙八部粒子插件的实现(1)
- PC-Lint软件使用方法
- GDB STL vector打印
- 获取下拉菜单的项文本 text
- vc win32 gdi+ 怎么显示PNG图片
- Json 简单介绍(转贴)
- Ogre 天龙八部地形管理器(2) --- 地形和静态对象的加载