laravel 代码的编写
<?php
namespace App\Http\Controllers\Admin\Official;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
define('TOKEN', 'yunlive11');
class WxresponseMsg extends Controller
{
protected $appid = 'wxecbfea5580fb894c';
protected $secret = 'daabe60820d93278cd1988e796b16fa5';
/**
* 自定义方法,用于微信访问验证开发者身份
*
* @param Request $request
* @return array|null|string
*/
public function wxtoken(Request $request)
{
$signature = $request->input('signature');
$timestamp = $request->input('timestamp');
$nonce = $request->input('nonce');
$echoStr = $request->input('echostr');
if ($this->checkSignature($signature, $timestamp, $nonce)) {
ob_end_clean(); //必须清空缓冲区
return $echoStr;
} else {
return 'Token verification failed.';
}
}
/**
* 微信官方提供的验签方法
*
* @param $signature
* @param $timestamp
* @param $nonce
* @return bool
*/
private function checkSignature($signature, $timestamp, $nonce)
{
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if ($tmpStr == $signature) {
return true;
} else {
return false;
}
}
// 微信事件推送接收方法 判断用户是否关注了公众号:
public function WxresponseMsg(){
$postStr = isset($GLOBALS["HTTP_RAW_POST_DATA"]) ? $GLOBALS["HTTP_RAW_POST_DATA"] : "";
$this->getdump($postStr);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
if(empty($postObj)){
// 当$postObj->Event == 'subscribe'(第一次关注时候)通过生成二维码的参数判断
if($postObj->Event == 'subscribe'){
// 关注
if(isset($postObj->EventKey) || isset($postObj->Ticket)){
// 扫描带参数二维码,未关注推送
$EventKey = $postObj->EventKey; //事件KEY值,qrscene_为前缀,后面为二维码的参数值
$Ticket = $postObj->Ticket; //二维码的ticket
}else{
// 普通关注
}
}
}
ob_end_clean(); //必须清空缓冲区这个只有在laravel里面应该要。其他不知
h这个创建的链接要post和get都可以请求
laravel修改方法
这个文件加入即可