php微信
来源:互联网 发布:腾讯云和阿里云的区别 编辑:程序博客网 时间:2024/05/25 08:13
<?phpnamespace Admin\Controller;use Think\Controller;class IndexController extends Controller { // 接入数据 public function index(){ $nonce = $_GET['nonce']; $timestamp = $_GET['timestamp']; $echostr = $_GET['echostr']; $signature = $_GET['signature']; $token = 'yin'; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature && $echostr ){ echo $echostr; exit; }else{ $this->reponseMsg(); } } // 接收事件推送并回复 public function reponseMsg(){ // 1.截取到微信推送过来的POST数据(XML格式) $postArr = $GLOBALS['HTTP_RAW_POST_DATA']; // 2.处理消息类型,并设置回复类型和类容 $postObj = simplexml_load_string($postArr); // 判断该数据包是否订阅的事件推送 if( strtolower($postObj->MsgType) == 'event'){ // 如果是关注subscribe事件 if( strtolower($postObj->Event == 'subscribe') ){ // 回复用户消息 $toUser = $postObj->FromUserName; $fromUser = $postObj->ToUserName; $time = time(); $msgType = 'text'; $content = '欢迎上车,嫩司机带你飞!回复:“福利” 有好东西'; $template = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content); echo $info; } } // 菜单点击回复 if( strtolower($postObj->Event) == 'click'){ if( strtolower($postObj->EventKey) == 'video'){ $content = '1111111111'; } if( strtolower($postObj->EventKey) == 'songs'){ $content = '222222222'; } $toUser = $postObj->FromUserName; $fromUser = $postObj->ToUserName; $time = time(); $msgType = 'text'; $template = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content); echo $info; } // 多图文回复 if( strtolower($postObj->MsgType) == 'text' && trim($postObj->Content) == '图文'){ $arr = array( array( 'title' => 'haha', 'description' => 'haha is haha', 'picurl' => 'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png', 'url' => 'https://www.baidu.com', ), array( 'title' => 'qq', 'description' => 'qq is qq', 'picurl' => 'http://mat1.gtimg.com/www/images/qq2012/qqlogo_1x.png', 'url' => 'http://www.qq.com', ), array( 'title' => 'haha', 'description' => 'haha is haha', 'picurl' => 'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png', 'url' => 'https://www.baidu.com', ), array( 'title' => 'qq', 'description' => 'qq is qq', 'picurl' => 'http://mat1.gtimg.com/www/images/qq2012/qqlogo_1x.png', 'url' => 'http://www.qq.com', ), ); $template = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <ArticleCount>". count($arr) ."</ArticleCount> <Articles>"; foreach($arr as $k => $v){ $template .= "<item> <Title><![CDATA[". $v['title'] ."]]></Title> <Description><![CDATA[". $v['description'] ."]]></Description> <PicUrl><![CDATA[". $v['picurl'] ."]]></PicUrl> <Url><![CDATA[". $v['url'] ."]]></Url> </item>"; } $template .= "</Articles> </xml> "; $toUser = $postObj->FromUserName; $fromUser = $postObj->ToUserName; $time = time(); $msgType = 'news'; $info = sprintf($template, $toUser, $fromUser, $time, $msgType); echo $info; }else{ // 纯文本回复 if( strtolower($postObj->MsgType) == 'text'){ switch( trim($postObj->Content)){ case '福利': $content = '叫声 爸爸 先'; break; case '爸爸': $content = '叫爸爸也不给'; break; default: $content = "<a href='http://www.baidu.com'>点我给你福利吧</a>"; break; } $template = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $toUser = $postObj->FromUserName; $fromUser = $postObj->ToUserName; $time = time(); $msgType = 'text'; $info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content); echo $info; } } } // 获取Access_Token public function getWxAccessToken(){ if( $_SESSION['access_token'] && $_SESSION['expire_time'] > time()){ // 如果access_token存在session并且没有过期 $access_token = $_SESSION['access_token']; return $access_token; }else{ // 1.请求URL地址 // 测试账号 $AppID = 'APPID'; $AppSecret = 'APPSECRET'; $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$AppID.'&secret='.$AppSecret; $res = $this->http_curl($url,'get','json'); $access_token = $res['access_token']; // 重新将获取到的Access_Token存到session $_SESSION['access_token'] = $access_token; $_SESSION['expire_time'] = time() + 7000; return $access_token; } } // 获取微信服务器IP地址 public function getWxServerIp(){ $access_token = $this->getWxAccessToken(); $url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token='.$access_token; $res = $this->http_curl($url,'get','json'); } // 数据请求 public function http_curl($url, $type = 'get', $res = 'json', $arr = ''){ $cl = curl_init(); curl_setopt($cl, CURLOPT_URL, $url); curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1); if($type == 'post'){ curl_setopt($cl, CURLOPT_POST, 1); curl_setopt($cl, CURLOPT_POSTFIELDS, $arr); } $output = curl_exec($cl); curl_close($cl); if($res == 'json'){ if( curl_error($cl)){ return curl_error($cl); }else{ return json_decode($output, true); } } } // 创建微信自定义菜单 public function WxMenu(){ $access_token = $this->getWxAccessToken(); $url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' . $access_token; $postArr = array( 'button' => array( //第一个一级菜单 array( 'name' => urlencode('菜单一'), 'type' => 'click', 'key' => 'video', ), //第二个一级菜单 array( 'name' => urlencode('菜单二'), 'sub_button' => array( array( 'name' => urlencode('歌曲'), 'type' => 'click', 'key' => 'songs', ), array( 'name' => urlencode('电影'), 'type' => 'view', 'url' => 'http://www.baidu.com', ), ), ), //第三个一级菜单 array( 'name' => urlencode('菜单三'), 'type' => 'view', 'url' => 'http://www.qq.com', ), ), ); $postJson = urldecode( json_encode($postArr) ); $res = $this->http_curl($url, 'post', 'json', $postJson); } // 群发接口 public function sendMsgAll(){ // 1.获取access__token $access_token = $this->getWxAccessToken(); $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token='.$access_token; $array = array( 'touser' => 'oyKMDxId-ksfGcZ8CWyxkOLrQI4U', 'text' => array('content' => 'wa ha ha',), 'msgtype' => 'text', ); $postJson = json_encode($array); $res = $this->http_curl($url, 'post', 'json', $postJson); var_dump($res); } // 模板消息 public function TempMsg(){ $access_token = $this->getWxAccessToken(); $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token; $array = array( 'touser' => 'oyKMDxId-ksfGcZ8CWyxkOLrQI4U', 'template_id' => 'X_61KMBtlzcEC8Wxa3qo1YTWMpLVWv7rTq2aQRXvrv8', 'url' => 'http://www.baidu.com', 'data' => array( 'name' => array('value' => 'nana', 'color' => '#173177'), 'money' => array('value' => 100, 'color' => '#173177'), 'date' => array('value' => date('Y-m-d H:i:s',time()), 'color' => '#173177'), ), ); $postJson = json_encode($array); $res = $this->http_curl($url, 'post', 'json', $postJson); var_dump($res); } // 网页授权 // 获取用户openid public function getBaseInfo(){ // 1.获取到code $appid = 'APPID'; $redirect_uri = urlencode('http://111.111.111.11/WeChat/Admin/index/getUserOpenId'); $url ='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect'; header('location:',$url); } public function getUserOpenId(){ // 获取到网页授权的access__token $appid = 'APPID'; $appsecret = 'APPSECRET'; $code = $_GET['code']; $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code'; $res = $this->http_curl($url); var_dump($res); } public function getUserDetail(){ // 1.获取到code $appid = 'APPID'; $redirect_uri = urlencode('http://111.111.111.11/WeChat/Admin/index/getUserInfo'); $url ='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'; header('location:',$url); } public function getUserInfo(){ // 获取到网页授权的access__token $appid = 'APPID'; $appsecret = 'APPSECRET'; $code = $_GET['code']; $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code'; $res = $this->http_curl($url); $access_token = $res['access_token']; $openid = $res['openid']; // 拉取用户详细信息 $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN'; $res = $this->http_curl($url); var_dump($res); }}
阅读全文
0 0
- php 微信1
- PHP 微信开发
- php微信支付
- 微信支付PHP
- php集成微信
- php微信支付
- php微信
- php微信
- php微信
- php微信
- php微信
- php微信
- php微信
- php微信
- php微信
- php开发微信
- PHP微信分享
- php开发微信demo
- 2.2节
- 2013计算机视觉代码合集三
- 工厂模式
- 抽象工厂模式
- 快速集成推送通知功能---百度云推送
- php微信
- codevs1262 不要把球传我
- [学习笔记]Hadoop安装_配置
- hihoCoder 1014 Trie树 字典树模板
- 适配器模式
- 考研数学笔记 56~60
- 权限管理--------分配权限
- OpenJudge 1996:登山——题解
- 装饰者模式