1. installation
Version 1.1 v-4.0 requires PHP version above 7.0
1.2 run the following command in the project directory
If composer is not installed, install composer first - > http://docs.phpcomposer.com/00-intro.html
Under windows environment, if openssl extension is missing is reported, modify php.ini file and open extension=php_openssl.dll
After the completion of the composer installation, you can execute it on the command line: composer config -g repo.packagist composer https://packagist.phpcomposer.com rewriting the Packagist image to the domestic image can speed up the download speed.
2. call
use EasyWeChat\Factory; class yourClass { // ...... // Start operation public function wechatAction() { $app = Factory::officialAccount(config('wechat_config')); // ... } }
'wechat_config' => [ /** * Debug Mode, bool value: true/false * * When the value is false, all logs will not be recorded */ 'debug' => true, /** * Basic account information, please get from wechat public platform / open platform */ 'app_id' => '', // AppID 'secret' => '', // AppSecret 'token' => '', // Token 'aes_key' => '', // EncodingAESKey,Please fill in the safe mode!!! /** * Log configuration * * level: Log level, optional: * debug/info/notice/warning/error/critical/alert/emergency * permission: Log file permission (optional), null by default (if it is null, the value of monolog will be 0644) * file: Log file location (absolute path!!!), requires writable permission */ 'log' => [ 'level' => 'debug', 'permission' => 0777, 'file' => LOG_PATH.'easywechat.log', ], /** * OAuth To configure * * scopes: Public platform (snsapi? Userinfo / snsapi? Base), open platform: snsapi? Login * callback: OAuth Callback page address after authorization */ 'oauth' => [ 'scopes' => ['snsapi_userinfo'], 'callback' => 'home/oauthallback', ], /** * WeChat payment */ 'payment' => [ 'merchant_id' => '', // Merchant number 'key' => '', 'cert_path' => '', // XXX: Absolute path!!!! 'key_path' => '', // XXX: Absolute path!!!! // 'device_info' => '013467007045764', // 'sub_app_id' => '', // 'sub_merchant_id' => '', // ... ], /** * Guzzle Global settings * * For more information, please refer to: http://docs.guzzlephp.org/en/latest/request-options.html */ 'guzzle' => [ 'timeout' => 3.0, // Timeout (seconds) 'verify' => true, // turn off SSL Certification (strongly not recommended!!!) ] ]
Before use, configure all parameters, and move to https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432
For example, obtaining 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 = "<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 "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; } } }
=========================After all configuration, you can play happily========================