ThinkPHP tp Codeigniter ci 微信开发 控制器代码 示例 实例 模拟http请求 获取acces_token 创建自定义菜单 点击事件 转多客服
Posted William Shaw技术博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ThinkPHP tp Codeigniter ci 微信开发 控制器代码 示例 实例 模拟http请求 获取acces_token 创建自定义菜单 点击事件 转多客服相关的知识,希望对你有一定的参考价值。
0
public function index(){ //echo $this->getAccessToken(); //$this->selectMenu(); //$this->creatMenu(); //$this->deleteMenu(); /* //用于验证的代码 勿动!! $tmpArr = array(‘xcjr2015‘, $_GET["timestamp"], $_GET["nonce"]); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr != $_GET["signature"] ){ exit; }else{echo $_GET["echostr"];exit;} */ $postStr = file_get_contents("php://input"); if ( empty( $postStr ) ){ exit; } $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA); $to = $postObj->ToUserName; $from = $postObj->FromUserName; $openid = sprintf(‘%s‘,$postObj->FromUserName); $content = sprintf(‘%s‘,$postObj->Content); $textTpl= "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $transferTpl=‘<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> </xml>‘; $news = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <ArticleCount>%s</ArticleCount> <Articles> <item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> </Articles> </xml>"; exit( sprintf( $textTpl, $from, $to, $_SERVER[‘REQUEST_TIME‘], ‘text‘,$openid) ); //接收消息///////////////// switch($postObj->MsgType){ case ‘event‘: switch(sprintf(‘%s‘,$postObj->Event)){ case ‘LOCATION‘: break; case ‘subscribe‘: break; case ‘scancode_waitmsg‘: break; case "CLICK": switch ($postObj->EventKey) { case "service": //exit( sprintf( $textTpl, $from, $to, $_SERVER[‘REQUEST_TIME‘], ‘text‘,$openid) ); //转到客服 $custom_url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->getAccessToken(); $customPostString = ‘{ "touser":"‘.$openid.‘", "msgtype":"text", "text": { "content":"正在接入...若客服不在请致电400-800-8957" } }‘; echo https_request($custom_url,$customPostString); exit( sprintf( $transferTpl, $from, $to, $_SERVER[‘REQUEST_TIME‘],‘transfer_customer_service‘) ); case ‘one_login‘: break; default: $content = "点击菜单:".$postObj->EventKey; return $this->transmitText($postObj, $content); } break; }//switch(sprintf(‘%s‘,$postObj->Event))结束 break; case ‘text‘: switch ($content) { case "测试": exit( sprintf( $textTpl, $from, $to, $_SERVER[‘REQUEST_TIME‘], ‘text‘,‘成功‘) ); case "kf": $custom_url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->getAccessToken(); $customPostString = ‘{ "touser":"‘.$openid.‘", "msgtype":"text", "text": { "content":"正常" } }‘; https_request($custom_url,$customPostString); break; default://转到客服 exit( sprintf( $transferTpl, $from, $to, $_SERVER[‘REQUEST_TIME‘],‘transfer_customer_service‘) ); } default://转到客服 exit( sprintf( $transferTpl, $from, $to, $_SERVER[‘REQUEST_TIME‘],‘transfer_customer_service‘) ); }//switch结束 }//index//////////////////////////方法结束 public function creatMenu()//创建菜单 { $menuPostString = //构造POST给微信服务器的菜单结构体 ‘{ "button":[ { "name":"我的账号", "sub_button":[ { "type":"view", "name":"用户登录", "url":"http://www.econgfin.com/mobile/login.html" }, { "type":"view", "name":"用户注册", "url":"http://www.econgfin.com/mobile/reg.html" }, { "type":"view", "name":"立即投标", "url":"http://www.econgfin.com/mobile/borrow/blist.html" }] }, { "name":"关于e葱", "sub_button":[ { "type":"view", "name":"公司简介", "url":"http://www.econgfin.com/mobile/site/gsjs.html" }, { "type":"view", "name":"联系我们", "url":"http://www.econgfin.com/mobile/site/contact.html" }, { "type":"view", "name":"加入e葱", "url":"http://www.econgfin.com/mobile/site/zhaopin.html" }] }, { "type":"view", "name":"新手指引", "url":"http://www.econgfin.com/mobile/safes/index.html", }] }‘; $menuPostUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->getAccessToken();//POST的url echo $this->https_request($menuPostUrl,$menuPostString); } public function deleteMenu(){//删除菜单 $MENU_URL="https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$this->getAccessToken(); echo $this->https_request($MENU_URL); } public function selectMenu(){//菜单 $MENU_URL="https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$this->getAccessToken(); echo $this->https_request($MENU_URL); } //获取Access Token/////// function getAccessToken() { //从mysql中获取access_token $query = $this->db->get(‘ych_token‘); $token = $query->row_array(); if(time() > ($token[‘last_time‘] + 7200)){ $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx9b5736a2e8185044&secret=c4a5797a7e8843d14dc0278eb0013dd8"; $res = $this->https_request($url); $result = json_decode($res, true); $data[‘access_token‘] = $result["access_token"]; $data[‘last_time‘] = time(); $this->db->where(‘id‘,1); $this->db->update(‘ych_token‘,$data); return $result["access_token"]; } return $token["access_token"]; } //https请求(支持GET和POST)///////////// function https_request($url, $data = null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; }
以上是关于ThinkPHP tp Codeigniter ci 微信开发 控制器代码 示例 实例 模拟http请求 获取acces_token 创建自定义菜单 点击事件 转多客服的主要内容,如果未能解决你的问题,请参考以下文章