订阅号 图文回复php,微信开发(PHP实现订阅号的公众号配置和自动回复)

首先在微信公众平台(网址:https://mp.weixin.qq.com)申请一个订阅号,然后在开发里找到开发者工具点击公众平台测试账号,在测试账号内进行微信开发实验。    1. 设置一个自己的有效的域名网址和TOKEN(就是暗号),TOKEN一定要与PHP代码中的TOKEN验证一致否则会一直配置失败(写有这段代码的文件一定要传到有效的域名网址内。与设置的网址必须相同)。    下面是PHP代码(在微信公众平台开发里的开发者文档内有部分代码) 。

//define your token

define("TOKEN", "weixin");

$wechatObj = new wechatCallbackapiTest();

$wechatObj->run();

class wechatCallbackapiTest

{

public function run()

{

if ($this->checkSignature()==false) {

die('非法请求');

}

if (isset($_GET["echostr"])) {

$echostr = $_GET["echostr"];

echo $echostr;

exit();

} else {

$this->responseMsg();

}

/*$echoStr = $_GET["echostr"];

//valid signature , option

if($this->checkSignature()){

echo $echoStr;

exit;*/

}

}

public function responseMsg()

{

//get post data, May be due to the different environments

$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

//extract post data

if (!empty($postStr)){

$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

$fromUsername = $postObj->FromUserName;

$toUsername = $postObj->ToUserName;

$keyword = trim($postObj->Content);

$time = time();

$textTpl = "

%s

0

";

if(!empty( $keyword ))

{

$msgType = "text";

$contentStr = "Welcome to wechat world!";

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

echo $resultStr;

}else{

echo "Input something...";

}

}else {

echo "";

exit;

}

}

private function checkSignature()

{

$signature = $_GET["signature"];

$timestamp = $_GET["timestamp"];

$nonce = $_GET["nonce"];

$token = TOKEN;

$tmpArr = array($token, $timestamp, $nonce);

sort($tmpArr);

$tmpStr = implode( $tmpArr );

$tmpStr = sha1( $tmpStr );

if( $tmpStr == $signature ){

return true;

}else{

return false;

}

}

}

?>

2.自动回复 首先重新写一个方法在上面那个文件夹里,再调用此方法,注意不能重名。以下代码是给订阅号发天气,它会回复天气晴朗,发放假,它会回复你“不好好奋斗瞎想啥?”回复其他则会回复"Welcome to wechat world!"public function responseMsg()

{

//get post data, May be due to the different environments

$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

file_put_contents('msg.txt', $postStr,FILE_APPEND);

//extract post data

if (!empty($postStr)){

$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

$fromUsername = $postObj->FromUserName;

$toUsername = $postObj->ToUserName;

$keyword = trim($postObj->Content);

$time = time();

//$msgType = $postObj->MsgType;//消息类型

//$event = $postObj->Event;//时间类型,subscribe(订阅)、unsubscribe(取消订阅)

$textTpl = "

%s

0

";

if(!empty( $keyword ))

{

$msgType = "text";

$contentStr = $this->autohuifu($keyword);

//$contentStr = "Welcome to wechat world!";

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

echo $resultStr;

}else{

echo "Input something...";

}

}else {

echo "";

exit;

}

}

public function autohuifu($keyword){

if($keyword =='天气'){

$contentStr = "天气晴朗";

}else if($keyword =='放假'){

$contentStr = "成天不好好奋斗瞎想啥?";

}else{

$contentStr = "Welcome to wechat world!";

}

return $contentStr;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值