1. 安裝php
1.1 v-4.0 版本要求 PHP版本在7.0以上html
1.2 在項目目錄下運行如下命令windows
若未安裝composer,則先安裝composer -> http://docs.phpcomposer.com/00-intro.htmlapi
windows 環境下安裝報 openssl extension is missing, 則修改php.ini文件,開啓extension=php_openssl.dll安全
composer安裝完成後能夠在命令行中執行:composer config -g repo.packagist composer https://packagist.phpcomposer.com
改寫Packagist 鏡像至國內鏡像能夠加快下載速度。
微信
2. 調用
app
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的獲取:composer
<?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; } } }
========================= 所有配置好後就能夠愉快的玩耍了 ========================post