PHP关注回复,微信公众号开发之(关注自动回复消息)

配置

20b04f839368d91eed94d93e10dfeac3.png

填写url和token  之后,在对应的目录下,写方法。。

47bc2ff742a777e9221911551c09ae73.png

在url这个目录下写方法

namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller {

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();

}

}

}

4.方法写好之后点击提交 配置成功后写自动回复的方法

// 接收事件推送并回复

public function reponseMsg(){

//1.获取到微信推送过来post数据(xml格式)

$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];

//2.处理消息类型,并设置回复类型和内容

/*

123456789

*/

$postObj = simplexml_load_string( $postArr );

//$postObj->ToUserName = '';

//$postObj->FromUserName = '';

//$postObj->CreateTime = '';

//$postObj->MsgType = '';

//$postObj->Event = '';

// gh_e79a177814ed

//判断该数据包是否是订阅的事件推送

if( strtolower( $postObj->MsgType) == 'event'){

//如果是关注 subscribe 事件

if( strtolower($postObj->Event == 'subscribe') ){

//回复用户消息(纯文本格式)

$toUser   = $postObj->FromUserName;

$fromUser = $postObj->ToUserName;

$time     = time();

$msgType  =  'text';

$content  = '欢迎关注我们的公众号,我们的网址是www.guyongtu.com';

$template = "

%s

";

$info     = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);

echo $info;

}

}

}

、、、、、、、、、、、、、、、、、、、、、、、、、、、、完整的代码。。。。。。。。。。。。。。。。。。。。。。。

namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller {

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'];

//2.处理消息类型,并设置回复类型和内容

/*

123456789

*/

$postObj = simplexml_load_string( $postArr );

//$postObj->ToUserName = '';

//$postObj->FromUserName = '';

//$postObj->CreateTime = '';

//$postObj->MsgType = '';

//$postObj->Event = '';

// gh_e79a177814ed

//判断该数据包是否是订阅的事件推送

if( strtolower( $postObj->MsgType) == 'event'){

//如果是关注 subscribe 事件

if( strtolower($postObj->Event == 'subscribe') ){

//回复用户消息(纯文本格式)

$toUser   = $postObj->FromUserName;

$fromUser = $postObj->ToUserName;

$time     = time();

$msgType  =  'text';

$content  = '欢迎关注我们的公众号,我们的网址是www.guyongtu.com';

$template = "

%s

";

$info     = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);

echo $info;

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值