php开发微信公众号代码封装
注意,php版本不要大于7.x,不然*$postStr = $GLOBALS[“HTTP_RAW_POST_DATA”] ;*这句代码会受影响;
<?php
define("token", "aaabbbccc");
define("appid", "假假,我爱过你!");
define("appsecret", "希望你过的更好!祝你幸福~~~");
// 注意php版本不要7.x以上
// 调用下面的方法
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
$wx = new wechatCallbackapiTest();
if (isset($_GET['echostr'])){
$wx->valid();
}else if(!empty($postStr)){
$wx->responseMsg();
}
//类
class wechatCallbackapiTest
{
//判断是微信服务器发来的还是用户发来的并作出反应
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
ob_clean();
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 == $s