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,这正是我想要的终于找到了正确的方法。拿来数据自己呈现,自己想怎么弄就怎么弄。于是开始编码

 

  1. 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>
  2. 第二步写后台代码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;
    }
    ?>
  3. 完成OK,这里用到了简单的文件缓存技术,在调用后台程序时,程序将检测我获取到的数据保存文件是否超过了一个小时的有效时间filemtime($weatherFile)+$lifetime)<time(),超过则调用webservice,否则直接调用数据保存文件,这样就能够提高效率