ylsunyuan技术论坛中心

 找回密码
 注册(请使用中文注册)
搜索
热搜: 活动 交友 discuz
查看: 866|回复: 0
打印 上一主题 下一主题

完整的微信天气查询模块 示例

[复制链接]

124

主题

127

帖子

619

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
619
跳转到指定楼层
楼主
发表于 2015-4-26 17:43:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
代码示例

[code]<?php
/**
* 天气查询模块处理程序
*
* @author sun
* @url http://bbs.ylsunyuan.com
*/
defined('IN_IA') or exit('Access Denied');

class Sun_weatherModuleProcessor extends WeModuleProcessor {
        public function respond() {
                $content = $this->message['content'];
                //这里定义此模块进行消息处理时的具体过程, 请查看微擎文档来编写你的代码
                preg_match("/^天气 (.*)/", $content, $res);
                $res = $this -> pinyin($res[1],utf8);

                $url = "http://apistore.baidu.com/microservice/weather?citypinyin=".$res;
                $json = $this -> curlGet($url);
                $res = json_decode($json,true);
                if(count($res['retData'])>0){
                        $content = $res['retData']['city']." ".$res['retData']['weather'].",气温:".$res['retData']['temp']."度,风向:".$res['retData']['WD'].",风力:".$res['retData']['WS']."。";
                       
                }else{
                        $content = "抱歉,无法识别您输入的城市名称!";
                }
                return $this->respText($content);
               
        }

        public function curlGet($url)
        {
                //$url = "http://apistore.baidu.com/microservice/weather?citypinyin=beijing";
                //初始化
                $ch = curl_init();
                //设置选项,包括URL
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                //执行并获取HTML文档内容
                $result = curl_exec($ch);
                //释放curl句柄
                curl_close($ch);
                //打印获得的数据
                return $result;
        }




        public function pinyin($_String, $_Code='UTF8')
        { //GBK页面可改为gb2312,其他随意填写为UTF8
        $_DataKey = "a|ai|an|ang|ao|ba|bai|ban|bang|bao|bei|ben|beng|bi|bian|biao|bie|bin|bing|bo|bu|ca|cai|can|cang|cao|ce|ceng|cha".
                        "|chai|chan|chang|chao|che|chen|cheng|chi|chong|chou|chu|chuai|chuan|chuang|chui|chun|chuo|ci|cong|cou|cu|".
                    
回复

使用道具 举报

本版积分规则

QQ|Archiver|手机版|小黑屋|ylsunyuan技术论坛 ( 桂ICP备14005218号-1

GMT+8, 2024-4-26 08:06 , Processed in 0.057309 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表