一、说明
1、建议在前几篇博客基础上看,特别是前一篇:
https://blog.csdn.net/john_rush/article/details/80641497
2、手册:
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140454
3、这里给出controller部分代码,模型里的代码在前面的博客里给过了:
https://blog.csdn.net/john_rush/article/details/80632349
二、主要代码:
//如果是重扫二维码
if (strtolower($postObj->Event == 'SCAN')){
if($postObj->EventKey == 2000){
//临时二维码扫码
$tmp = '临时二维码欢迎你';
}elseif($postObj->EventKey == 3000){
//永久二维码扫码
$tmp = '永久二维码欢迎你';
}
$arr = array(
array(
'title'=>$tmp,
'description'=>"欢迎来到放哥的公众号",
'picUrl'=>'http://www.imooc.com/static/img/common/logo.png',
'url'=>'http://www.imooc.com',
),
);
$model = new \Weixin\Model\IndexModel();
$model -> responseSubscribe($postObj, $arr);
}
}
三、全部代码
<?php
namespace Weixin\Controller;
use Think\Controller;
class IndexController extends Controller{
public function __construct(){
}
public function index(){
//获得参数 signature nonce token timestamp echostr
$nonce = $_GET['nonce'];
$token = 'weixin';
$timestamp = $_GET['timestamp'];
$echostr = $_GET['echostr'];
$signature = $_GET['signature'];
//形成数组,然后按字典序排序
$array = array();
$array = array($nonce, $timestamp, $token);
sort($array);
//拼接成字符串,sha1加密 ,然后与signature进行校验
$str = sha1( implode( $array ) );
if( $str == $signature && $echostr ){
//第一次接入weixin api接口的时候
echo $echostr;
exit;
}else{
$this->reponseMsg();
}
}
// 接收事件推送并回复
public function reponseMsg(){
//1.获取到微信推送过来post数据(xml格式)
//$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];//php7开始不支持
$postArr = file_get_contents("php://input");
//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 = '欢迎关注我们的微信公众账号'.$postObj->FromUserName.'-'.$postObj->ToUserName;
$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; */
//回复一个单图文
$arr = array(
array(
'title'=>'程序员的天堂',
'description'=>"欢迎来到放哥的公众号",
'picUrl'=>'http://www.imooc.com/static/img/common/logo.png',
'url'=>'http://www.imooc.com',
),
);
$model = new \Weixin\Model\IndexModel();
$model -> responseSubscribe($postObj, $arr);
}
//如果是重扫二维码
if (strtolower($postObj->Event == 'SCAN')){
if($postObj->EventKey == 2000){
//临时二维码扫码
$tmp = '临时二维码欢迎你';
}elseif($postObj->EventKey == 3000){
//永久二维码扫码
$tmp = '永久二维码欢迎你';
}
$arr = array(
array(
'title'=>$tmp,
'description'=>"欢迎来到放哥的公众号",
'picUrl'=>'http://www.imooc.com/static/img/common/logo.png',
'url'=>'http://www.imooc.com',
),
);
$model = new \Weixin\Model\IndexModel();
$model -> responseSubscribe($postObj, $arr);
}
}
//dump($postObj->Event);
//dump($postObj->Eventkey);
//自定义菜单中event->click事件
if( strtolower($postObj->Event) == 'click'){
$key = (string)$postObj->EventKey;
if(strtolower($key == 'item1')){
$content = "这是item1菜单的事件推送";
}elseif(strtolower($key == 'songs')){
$content = "这是歌曲菜单的事件推送";
} else {
$content = '未匹配'.print_r($postObj->EventKey, true);
}
$model = new \Weixin\Model\IndexModel();
$model -> responseText($postObj, $content);
}
//如果是自定义菜单中的event->view
if(strtolower($postObj->Event) == 'view'){
$content="跳转的链接是".$postObj->EventKey;
$model = new \Weixin\Model\IndexModel();
$model -> responseText($postObj, $content);
}
//用户发送tuwen1关键字的时候,回复一个单图文
if( strtolower($postObj->MsgType) == 'text' && trim($postObj->Content)=='tuwen2' ){
//从数据库中查询得到
$arr = array(
array(
'title'=>'imooc',
'description'=>"imooc is very cool",
'picUrl'=>'http://www.imooc.com/static/img/common/logo.png',
'url'=>'http://www.imooc.com',
),
array(
'title'=>'hao123',
'description'=>"hao123 is very cool",
'picUrl'=>'https://www.baidu.com/img/bdlogo.png',
'url'=>'http://www.hao123.com',
),
array(
'title'=>'qq',
'description'=>"qq is very cool",
'picUrl'=>'http://www.imooc.com/static/img/common/logo.png',
'url'=>'http://www.qq.com',
),
);
//实例化模型
$model = new \Weixin\Model\IndexModel();
$model -> responseNews($postObj, $arr);
//注意:进行多图文发送时,子图文个数不能超过10个
}else{
switch(trim($postObj->Content)){
case 1:
$content = '您输入的数字是1';
break;
case 2:
$content = '您输入的数字是2';
break;
case 3:
$content = '您输入的数字是3';
break;
case 4:
$content = "<a href='http://www.imooc.com'>慕课</a>";
break;
case '英文':
$content = 'imooc is ok';
break;
default:
$content = '没有找到相关信息';
}
//实例化模型
$model = new \Weixin\Model\IndexModel();
$model -> responseText($postObj, $content);
}//if end
}//reponseMsg end
/*
*$url 接口url string
*$type 请求类型 string
*$res 返回数据类型 string
*%$arr post 请求参数 string
*/
public function http_curl($url,$type='get',$res='json',$arr=''){
//1.初始化curl
$ch = curl_init();
//2.设置curl的参数
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($type == 'post'){
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
}
//3.采集
$output = curl_exec($ch);
//4.关闭
curl_close($ch);
if($res=='json'){
if(curl_error($ch)){
//请求失败,返回错误信息
return curl_error($ch);
}else{
//请求成功,返回错误信息
return json_decode($output,true);
}
}
}
/*
*返回access_token *session解决办法 存mysql memcache
*/
public function getWxAccessToken(){
//将access_token 存在session/cookie中
if($_SESSION['access_token'] && $_SESSION['expire_time'] > time()){
//如果access_token在session中没有过期
return $_SESSION['access_token'];
}else{
//如果access_token在session中不存在或者已经过期
$appid = 'wxb20bebb764546087';
$appsecret = '64e22d215895d52af21c8fcbf6da3241';
$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['erpire_time'] = time() + 7000;
return $access_token;
}
}
//生成临时二维码
public function getTimeQrCode(){
//1、获取ticket票据
//全局票据access_token 网页授权access_token 微信js_SDK jsapi_ticket
$access_token = $this->getWxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
$postArr = array(
'expire_seconds' => 604800,
'action_name' => "QR_SCENE",
'action_info' => array(
'scene' =>array('scene_id' => 2000),
),
);
$postJson = json_encode($postArr);
$res = $this->http_curl($url,'post','json',$postJson);
//var_dump($res);
$ticket = $res['ticket'];
//var_dump($ticket);
//2、使用ticket获取二维码图片
$url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
echo "<img src='".$url."' />";
}
//生成永久二维码
public function getForeverQrCode(){
//1、获取ticket票据
//全局票据access_token 网页授权access_token 微信js_SDK jsapi_ticket
$access_token = $this->getWxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
$postArr = array(
'action_name' => "QR_LIMIT_SCENE",
'action_info' => array(
'scene' =>array('scene_id' => 3000),
),
);
$postJson = json_encode($postArr);
$res = $this->http_curl($url,'post','json',$postJson);
//var_dump($res);
$ticket = $res['ticket'];
//var_dump($ticket);
//2、使用ticket获取二维码图片
$url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
echo "<img src='".$url."' />";
}
}//class end
《贼王》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/45243.html
真好呢