普通版本
<?php
define("WEIXINTOKEN",'weixin1809B');
$wechatobj = new WechatApiTest();
$wechatobj->valid();
Class WechatApiTest
{
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 = WEIXINTOKEN;
$arr = array($token,$timestamp,$nonce);
sort($arr,SORT_STRING);
$str = implode($arr);
$sign = sha1($str);
if($sign == $signature){
return true;
}else{
return false;
}
}
}
laravel框架版本
<?php
namespace App\Http\Controllers\Wechat;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Wechat;
class WechatController extends Controller
{
public function check(Request $request)
{
if (!empty($request->echostr)) {
if ($this->CheckSignature()) {
echo $request->echostr;
exit;
}
} else {
$this->responseMsg();
}
}
private function CheckSignature()
{
$signature = $_GET['signature'];
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
$token = env("WEIXINTOKEN");