您好,欢迎访问一九零五行业门户网

基于微信公众平台开发SDK(demo)扩展例子

该扩展基于官方的微信公众平台sdk,这里只做了简单地封装,实现了一些基本的功能,如天气查询,翻译,自动聊天机器人,自定义菜单接口)仅供学习之用.代码如下:
responsemsg();class wechatcallbackapitest { //签名验证公共接口 public function valid() { $echostr = $_get[echostr]; if ($this->checksignature()) { echo $echostr; exit; } } //主入口处理函数 public function responsemsg() { $poststr = $globals[http_raw_post_data]; if (!emptyempty($poststr)) { $postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata); $msgtype = trim($postobj->msgtype); switch ($msgtype) { case 'text': $resultstr = $this->handletext($postobj); break; case 'event': $resultstr = $this->handleevent($postobj); break; default: $resultstr = $this->handledefault($postobj); break; } echo $resultstr; } else { echo error; exit; } } //处理文本消息 private function handletext($obj) { $keyword = trim($obj->content); if (preg_match('/天气/', $keyword)) { $contentstr = $this->handleweather($obj); } elseif (preg_match('/翻译/', $keyword)) { $contentstr = $this->handletranslation($obj); } else { $contentstr = $this->handlechat($obj); } return $this->handlestr($obj, $contentstr); } //处理天气 private function handleweather($obj) { $keyword = mb_substr($obj->content, -2, 2, 'utf-8'); $zone = mb_substr($obj->content, 0, -2, 'utf-8'); if ($keyword == '天气' && !emptyempty($zone)) { $zonearr = json_decode(file_get_contents('http://api.k780.com:88/?app=weather.city&format=json') , true); $zonearr = $zonearr['result']; $cityid = ''; foreach ($zonearr as $value) { if ($zone == $value['citynm']) { $cityid = $value['weaid']; break; } } if (!emptyempty($cityid)) { $data = file_get_contents(http://api.k780.com:88/?app=weather.today&weaid=$cityid&appkey=10638&sign=3736578f099375665f9f141a6326b757&format=json); $data = json_decode($data); $contentstr = 今天是: . $data->result->days . , . $data->result->week . , . $data->result->citynm . 天气: . $data->result->weather . n温度: . $data->result->temperature . , . $data->result->wind_direction . , . $data->result->wind_power . , 最低温度: . $data->result->temp_low . ,最高温度: . $data->result->temp_high; } else { $contentstr = '找不到输入的城市!'; } } else { $contentstr = '输入的查询格式不正确!'; } return $contentstr; } //处理翻译 private function handletranslation($obj) { $keyword = mb_substr($obj->content, 0, 2, 'utf-8'); $words = mb_substr($obj->content, 2, 220, 'utf-8'); if ($keyword == '翻译' && !emptyempty($words)) { $data = file_get_contents('http://fanyi.youdao.com/openapi.do?keyfrom=zfsblog&key=364295447&type=data&doctype=json&version=1.1&q=' . urlencode($words)); $data = json_decode($data, true); switch ($data['errorcode']) { case '0': $contentstr = $data['translation'][0]; break; case '20': $contentstr = '要翻译的文本过长'; break; case '30': $contentstr = '无法进行有效的翻译'; break; case '40': $contentstr = '不支持的语言类型'; break; case '50': $contentstr = '无效的key'; break; default: $contentstr = 'error'; break; } } else { $contentstr = '输入的翻译格式不正确!'; } return $contentstr; } //处理聊天信息 private function handlechat($obj) { $keywords = $obj->content; $curlpost = array( chat => $keywords ); $ch = curl_init(); //初始化curl curl_setopt($ch, curlopt_url, 'http://www.xiaojo.com/bot/chata.php'); //抓取指定网页 curl_setopt($ch, curlopt_header, 0); //设置header curl_setopt($ch, curlopt_returntransfer, 1); //要求结果为字符串且输出到屏幕上 curl_setopt($ch, curlopt_post, 1); //post提交方式 curl_setopt($ch, curlopt_postfields, $curlpost); $data = curl_exec($ch); //运行curl curl_close($ch); if (!emptyempty($data)) { $contentstr = $data; } else { $ran = rand(1, 5); switch ($ran) { case 1: $contentstr = 小九今天累了,明天再陪你聊天吧; break; case 2: $contentstr = 小九睡觉喽~~; break; case 3: $contentstr = 呼呼~~呼呼~~; break; case 4: $contentstr = 你话好多啊,不跟你聊了; break; case 5: $contentstr = 你话好多啊,不跟你聊了; break; } } return $contentstr; } //创建自定义菜单 public function createmenu($data, $token) { $ch = curl_init(); curl_setopt($ch, curlopt_url, https://api.weixin.qq.com/cgi-bin/menu/create?access_token= . $token); curl_setopt($ch, curlopt_customrequest, post); curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_ssl_verifyhost, false); curl_setopt($ch, curlopt_useragent, 'mozilla/5.0 (compatible; msie 5.01; windows nt 5.0)'); curl_setopt($ch, curlopt_followlocation, 1); curl_setopt($ch, curlopt_autoreferer, 1); curl_setopt($ch, curlopt_postfields, $data); curl_setopt($ch, curlopt_returntransfer, true); $result = curl_exec($ch); curl_close($ch); return $result; } //查询自定义菜单 function getmenu($token) { $url = https://api.weixin.qq.com/cgi-bin/menu/get?access_token=$token; $ch = curl_init($url); curl_setopt($ch, curlopt_returntransfer, true); //获取数据返回 curl_setopt($ch, curlopt_binarytransfer, true); //在启用 curlopt_returntransfer 时候将获取数据返回 curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_ssl_verifyhost, false); return $output = curl_exec($ch); } //删除自定义菜单 public function deletemenu($token) { $url = https://api.weixin.qq.com/cgi-bin/menu/delete?access_token= . $token; $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_ssl_verifyhost, false); $result = curl_exec($ch); curl_close($ch); return $result; } //处理事件消息 private function handleevent($obj) { $content = ''; switch ($obj->event) { case 'subscribe': $content.= welcome-欢迎关注该公众号号!; break; case 'unsubscribe': $content.= 感谢您一直以来对该公众号的关注,再见!; break; default: $content.= ; break; } return $this->handlestr($obj, $content); } //处理回复消息字符串 private function handlestr($obj, $content = '', $flag = 0) { $texttpl = ; return sprintf($texttpl, $obj->fromusername, $obj->tousername, time() , $content, $flag); } //签名验证函数 private function checksignature() { $signature = $_get[signature]; $timestamp = $_get[timestamp]; $nonce = $_get[nonce]; //开源代码phprm.com $token = token; $tmparr = array( $token, $timestamp, $nonce ); sort($tmparr, sort_string); $tmpstr = implode($tmparr); $tmpstr = sha1($tmpstr); if ($tmpstr == $signature) { return true; } else { return false; } }}
文章链接:
随便收藏,请保留本文地址!
其它类似信息

推荐信息