前言:
由于最近安装了傻妞机器人,对接微信公众号的时候一直是获取Token失败,经过研究解决了。
我是云服务器安装了宝塔,是占用了80端口,所以先清除80端口的占用。
清除80端口占用:
sof -i :80|grep -v “PID”|awk ‘{print “kill -9”,$2}’|sh
微信公众号后台
地址:https://mp.weixin.qq.com/
选择 开发→基本配置
然后点击修改配置
到这里的时候点提交依旧是验证失败,别急看下面
下载我提供的PHP文件,把这个PHP文件扔到域名wx目录下(自己创建一个名叫wx的目录)
下载地址:点我下载
附上代码,不想下载的自己新建一个文档复制进去然后改名index.php 即可
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "修改我跟token一致");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest{
public function valid(){
$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 = "<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(!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;
}
}
}
?>
注意:下载的PHP文件一定要修改自己的TOKEN值为自己设定的值。
现在点提交是不是验证通过了呢?
如果我的文章对你有帮助,欢迎移至上方按钮打赏
© 版权声明
THE END