1. 安装
1.1 v-4.0 版本要求 PHP版本在7.0以上
1.2 在项目目录下运行以下命令
若未安装composer,则先安装composer -> http://docs.phpcomposer.com/00-intro.html
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 认证(强烈不建议!!!) ] ]
使用之前,要先配置好各个参数, 具体移步至 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432
例如 token的获取:
php 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 = ""; 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; } } } %s 0
========================= 全部配置好后就可以愉快的玩耍了 ========================