1.去https://ngrok.com/上面注册账号
2.下载ngrok软件
3.配置Authtoken 运行命令 ./ngrok authtoken xxxxxxxxxxxxxxxx
4.配置完毕后,启动 ./ngrok http 80
5.去微信https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index 测试账号平台登陆微信
6.接口配置信息,URL,Token都必须填写
7.编写接受微信测试账号平台参数代码
<?php
define('TOKEN', 'weixin');
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
file_put_contents('weixin_log.txt', "IP=".$_SERVER['REMOTE_ADDR'].PHP_EOL,FILE_APPEND);
if ($this->checkSignature($signature, $timestamp, $nonce)) {
header('content-type:text');
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
ini_set('always_populate_raw_post_data', -1);
$postStr = file_get_contents('php://input');
file_put_contents('weixin_log.txt', var_export($postStr).PHP_EOL,FILE_APPEND);
//$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$postObj = json_encode($postObj);
$postObj = json_decode($postObj,true);
$fromUsername = $postObj['FromUserName'];
$toUsername = $postObj['ToUserName'];
$keyword = trim($postObj['Content']);//用户发送的消息
$msgType = $postObj['MsgType'];//消息类型
$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, $timestamp, $nonce)
{
$tmpArr = array(TOKEN, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if ($tmpStr == $signature) {
return true;
} else {
return false;
}
}
}
8.点击保存。保存时会碰到配置失败,查看代码是否有误或者ngrok有问题
(Sunny-Ngrok的ngrok配置不成功,改用ngrok.com的一下就配置成功)
9.接下来你可以再设置一些公众号参数。比如“JS接口安全域名”,“网页授权获取用户基本信息”
对此微信公众号测试账号配置完毕