Easywechat-used in thinkPHP5,
1. Install
1.1 v-4.0 version requires PHP version above 7.0
1.2 run the following command in the project directory:
If composer is not installed, install composer-> http://docs.phpcomposer.com/00-intro.html first
If openssl extension is missing is reported during installation in windows, modify the php. ini file and enable extension = php_openssl.dll.
After the composer is installed, run the following command in the command line:composer config -g repo.packagist composer https://packagist.phpcomposer.com Downloading Packagist images to domestic images can be accelerated.
2. Call
Use EasyWeChat \ Factory; class yourClass {//...... // start to operate the public function wechatAction () {$ app = Factory: officialAccount (config ('wechat _ config '));//...}}
'Wechat _ config' => [/*** Debug mode, bool value: true/false ** when the value is false, all logs do not record */'debug' => true,/*** basic account information, obtain */'app _ id' => ''from the public platform/Open Platform, // AppID 'secret' => '', // AppSecret 'Token' => '', // token 'aes _ key' =>'', // EncodingAESKey. Be sure to enter it in safe mode !!! /*** Log configuration ** level: Log level. Optional values: * debug/info/notice/warning/error/critical/alert/emergency * permission: log file Permission (Optional). The default value is null (if it is null, monolog will take 0644) * file: Log file Location (absolute path !!!), Write permission required */'log' => ['level' => 'debug', 'permission' => 0777, 'file' => LOG_PATH. 'easywechat. log',],/*** OAuth configuration ** scopes: public platform (snsapi_userinfo/snsapi_base), Open Platform: snsapi_login * callback: the callback page address after OAuth authorization is completed */'oauth' => ['scopes '=> ['snsapi _ userinfo'], 'callback' => 'home/oauthallback',],/*** payment */'payment' => ['Merchant _ id' => '', // merchant ID 'key' => '', 'cert _ path' =>'', // XXX: absolute path !!!! 'Key _ path' => '', // XXX: absolute path !!!! // 'Device _ info' => '123', // 'sub _ app_id '=> '', // 'sub _ merchant_id' => '', //...], /*** Guzzle global settings ** For more information, see: http://docs.guzzlephp.org/en/latest/request-options.html */'guzzle' => ['timeout' => 3.0, // timeout (seconds) 'verify '=> true, // disable SSL authentication (not recommended !!!)]
Before using, you need to configure each parameter, specific steps to the https://mp.weixin.qq.com/wiki? T = resource/res_main & id = mp1445241432
For example, get the token:
<?phpnamespace 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 the configurations are complete, you can enjoy the fun of playing ====== ==============================