easywechat php例子,easywechat--在thinkPHP5中的使用

1. 安装

1.1 v-4.0 版本要求 PHP版本在7.0以上

1.2 在项目目录下运行以下命令

0519c8b2d4dd8c8a59f48f9226750c6d.png

windows 环境下安装报 openssl extension is missing, 则修改php.ini文件,开启extension=php_openssl.dll

composer安装完成后可以在命令行中执行:composer config -g repo.packagist composer https://packagist.phpcomposer.com改写Packagist 镜像至国内镜像可以加快下载速度。

2. 调用

use EasyWeChat\Factory;

class yourClass {

// ......

// 开始操作

public function wechatAction() {

$app = Factory::officialAccount(config('wechat_config'));

// ...

}

}

'wechat_config' => [

/**

* Debug 模式,bool 值:true/false

*

* 当值为 false 时,所有的日志都不会记录

*/

'debug' => true,

/**

* 账号基本信息,请从微信公众平台/开放平台获取

*/

'app_id' => '', // AppID

'secret' => '', // AppSecret

'token' => '', // Token

'aes_key' => '', // EncodingAESKey,安全模式下请一定要填写!!!

/**

* 日志配置

*

* level: 日志级别, 可选为:

* debug/info/notice/warning/error/critical/alert/emergency

* permission:日志文件权限(可选),默认为null(若为null值,monolog会取0644)

* file:日志文件位置(绝对路径!!!),要求可写权限

*/

'log' => [

'level' => 'debug',

'permission' => 0777,

'file' => LOG_PATH.'easywechat.log',

],

/**

* OAuth 配置

*

* scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login

* callback:OAuth授权完成后的回调页地址

*/

'oauth' => [

'scopes' => ['snsapi_userinfo'],

'callback' => 'home/oauthallback',

],

/**

* 微信支付

*/

'payment' => [

'merchant_id' => '', // 商户号

'key' => '',

'cert_path' => '', // XXX: 绝对路径!!!!

'key_path' => '', // XXX: 绝对路径!!!!

// 'device_info' => '013467007045764',

// 'sub_app_id' => '',

// 'sub_merchant_id' => '',

// ...

],

/**

* Guzzle 全局设置

*

* 更多请参考: http://docs.guzzlephp.org/en/latest/request-options.html

*/

'guzzle' => [

'timeout' => 3.0, // 超时时间(秒)

'verify' => true, // 关掉 SSL 认证(强烈不建议!!!)

]

]

例如 token的获取:

namespace app\home\controller;

class Access extends Home {

public $token = 'yourToken';

public function index()

{

$echoStr = input('param.echostr');

if( $this->checkSignature() ){

echo $echoStr;

exit;

}

}

public function responseMsg()

{

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

$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

//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);

$time = time();

$textTpl = "

%s

0

";

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 "test,,,,";

exit;

}

}

private function checkSignature()

{

$param = input('param.');

$signature = $param["signature"];

$timestamp = $param["timestamp"];

$nonce = $param["nonce"];

$tmpArr = array( $this->token, $timestamp, $nonce );

// use SORT_STRING rule

sort($tmpArr, SORT_STRING);

$tmpStr = implode( $tmpArr );

$tmpStr = sha1( $tmpStr );

if( $tmpStr == $signature ){

return true;

}else{

return false;

}

}

}

========================= 全部配置好后就可以愉快的玩耍了 ========================

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值