1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 微信公众号连接自己的服务器 微信公众号开发之配置和绑定自己的服务器

微信公众号连接自己的服务器 微信公众号开发之配置和绑定自己的服务器

时间:2018-08-20 11:42:49

相关推荐

微信公众号连接自己的服务器 微信公众号开发之配置和绑定自己的服务器

1.在开始之前我们首先要有一个域名(www.wyzdjg.top)和公众号(这里是订阅号)。

2.我们首先在服务器建立一个weixin文件夹,里面有一个index.php文件,如下图:

然后我们在index.php文件中写入以下代码,然后上传:

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 = "%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;

}

}

}

?>

最后我们在微信那配置会显示:

这样就说明已经配置成功。

然后我们将index.php中的

$wechatObj->valid()改为$wechatObj->responseMsg();

然后我们在上传。在微信扫描测试:

这样就说明微信公众号与你自己的服务器配置成功。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。