WeChat public number token has been verified successfully, but automatic response is not responding.
WeChat public number token has been verified successfully, but automatic response is not responding.
WeChat public number token has been verified successfully, but automatic response is not responding.
Start with the code and enter "auto reply time"
<?php
header('Content-type:text');
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET['echostr'])) {
$wechatObj->valid();
}else{
$wechatObj->responseMsg();
}
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
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;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_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 = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
if($keyword == "?" || $keyword == "?")
{
$msgType = "text";
$content = date("Y-m-d H:i:s",time());
$result = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $content);
echo $result;
}
}else{
echo "";
exit;
}
}
}
?>
Reason: $poststr = $globals ["HTTP raw post data"]; cannot get value, empty
1. Replace $globals ["http \ raw \ post \ data"] with file \ get \ contents ('php: / / input '); to obtain data test. If it is normal, it means that it is the error caused by the value not obtained above. If it is still not available, it may be the data transmission error
2. If the file "get" contents ('php: / / input ') above is normal, check the php.ini configuration file and configure "always" popular "raw" post "data = on. [note] this processing method is only available in PHP ini when the PHP version is lower. Versions above 7 have been abolished
If there is a fault in the public service, it can be debugged with the echo "echo"; because the server has nothing to return without response, rather than the service failure of the public number.