订阅号 图文回复php,微信开发(PHP实现订阅号的公众号配置和自动回复)
首先在微信公眾平臺(tái)(網(wǎng)址:https://mp.weixin.qq.com)申請(qǐng)一個(gè)訂閱號(hào),然后在開(kāi)發(fā)里找到開(kāi)發(fā)者工具點(diǎn)擊公眾平臺(tái)測(cè)試賬號(hào),在測(cè)試賬號(hào)內(nèi)進(jìn)行微信開(kāi)發(fā)實(shí)驗(yàn)。? ? 1. 設(shè)置一個(gè)自己的有效的域名網(wǎng)址和TOKEN(就是暗號(hào)),TOKEN一定要與PHP代碼中的TOKEN驗(yàn)證一致否則會(huì)一直配置失敗(寫(xiě)有這段代碼的文件一定要傳到有效的域名網(wǎng)址內(nèi)。與設(shè)置的網(wǎng)址必須相同)。? ? 下面是PHP代碼(在微信公眾平臺(tái)開(kāi)發(fā)里的開(kāi)發(fā)者文檔內(nèi)有部分代碼) 。
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->run();
class wechatCallbackapiTest
{
public function run()
{
if ($this->checkSignature()==false) {
die('非法請(qǐng)求');
}
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.自動(dòng)回復(fù)?首先重新寫(xiě)一個(gè)方法在上面那個(gè)文件夾里,再調(diào)用此方法,注意不能重名。以下代碼是給訂閱號(hào)發(fā)天氣,它會(huì)回復(fù)天氣晴朗,發(fā)放假,它會(huì)回復(fù)你“不好好奮斗瞎想啥?”回復(fù)其他則會(huì)回復(fù)"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;//消息類(lèi)型
//$event = $postObj->Event;//時(shí)間類(lèi)型,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;
總結(jié)
以上是生活随笔為你收集整理的订阅号 图文回复php,微信开发(PHP实现订阅号的公众号配置和自动回复)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Win11鼠标动不了如何恢复?Win11
- 下一篇: php 递归分销角色,PHP递归无限分类