php微信服务器配置,微信公众号开发之服务器配置(接口配置信息)

暂以申请的测试号为例:

1. 在params.php文件中填写微信的参数(必须跟服务器填写或生成的完全一致):'wechatConfig' => [

'app_id' => 'wxe04f591fa3c7cbe1', // AppID

'secret' => 'f41bc4a254360d85692c0fb517d12ada', // AppSecret

'token' => 'wxe04f591fa3c7cbe1', // Token

],

2. 控制器(SiteController.php):<?php

namespace frontend\controllers;

use Yii;

use yii\base\InvalidConfigException;

use yii\web\Controller;

use yii\web\NotFoundHttpException;

class SiteController extends Controller

{

public $enableCsrfValidation = false;

/**

* 微信接口

* @return string

* @throws InvalidConfigException

* @throws NotFoundHttpException

*/

public function actionWechat()

{

$request = Yii::$app->request;

switch($request->getMethod()){

case 'GET':

if(self::checkSignature($request->get('signature'), $request->get('timestamp'), $request->get('nonce'))){

echo $request->get('echostr'); exit();

}else{

throw new NotFoundHttpException('签名验证失败.');

}

break;

case 'POST':

// 处理消息回复

break;

default:

throw new NotFoundHttpException('所请求的页面不存在.');

}

exit();

}

/**

* 参数校验

* @param string $signature

* @param int $timestamp

* @param int $nonce

* @return bool

* @throws InvalidConfigException

*/

private function checkSignature($signature, $timestamp, $nonce)

{

$token = Yii::$app->params['wechatConfig']['token'];

if($token){

$tmpArr = [$token, $timestamp, $nonce];

sort($tmpArr, SORT_STRING);

$newSignature = sha1(implode($tmpArr));

return $newSignature == $signature;

}else{

throw new InvalidConfigException('TOKEN is not defined!');

}

}

}

2.1 控制器中如果使用了动作过滤器,还要添加:public function behaviors()

{

return [

'access' => [

'class' => AccessControl::className(),

'rules' => [

[

'actions' => ['login', 'error', 'wechat'],

'allow' => true,

],

[

'actions' => ['logout', 'index'],

'allow' => true,

'roles' => ['@'],

],

],

],

];

}

3.填写微信公众号的服务器配置:

4c367271254ec2094288af9fdd116684.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值