微信公衆號url認證(服務器認證)

把下面的代碼複製

路由需指定到wx方法

<?php

 

namespace App\Http\Controllers\Wx;

 

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

 

class WxController extends Controller

{

public function Wx(){

//valid signature , option

if($this->checkSignature()){

$echoStr = $_GET["echostr"];

echo $echoStr;

exit;

}

}

public function responseMsg()

{

//get post data, May be due to the different environments

// $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

$postStr = file_get_contents("php://input");

//extract post data

if (!empty($postStr)){

/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,

the best way is to check the validity of xml by yourself */

libxml_disable_entity_loader(true);

$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

$fromUsername = $postObj->FromUserName;

$toUsername = $postObj->ToUserName;

$keyword = trim($postObj->Content);

$msgType=$postObj->MsgType;

$event=$postObj->Event; //接收事件類型

$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;

}

if($msgType=="event"){

if($event=="subscribe"){

$msgType = "text";

$contentStr = "Welcome to wechat world-----subscribe!";

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

echo $resultStr;

header("Location:http://case.jujiaoweb.com/index/index/sub");

}

}

}

private function checkSignature(){

$signature = $_GET["signature"];

$timestamp = $_GET["timestamp"];

$nonce = $_GET["nonce"];

$token = 'weixin2019';//服務器配置的令牌token

$tmpArr = array($token, $timestamp, $nonce);

sort($tmpArr);

$tmpStr = implode( $tmpArr );

$tmpStr = sha1( $tmpStr );

if( $tmpStr == $signature ){

return true;

}else{

return false;

}

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章