? <PHP / ** * micro-channel sharing relevant * * @Since 2017-07-15 * / use YAF \ Registry; class Service_WxShare { Private $ appId; Private $ appsecret; public function __construct ($ appId, $ appsecret) { $ the this -> = $ for appId for appId; $ this-> = $ appsecret appsecret; } public getSignPackage function () { $ jsapiTicket = $ this-> getJsApiTicket (); // URL must be noted that dynamic access, not the hardcode. $ = Protocol ( !!? empty ($ _ SERVER [ 'HTTPS']) && $ _SERVER [ 'HTTPS'] == 'off' || $ _SERVER [ 'SERVER_PORT'] == 443) "https: //": "http: / / "; $ url ="$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $ timestamp = Time (); $ nonceStr = $ this-> createNonceStr (); order // here parameters in accordance with the key value of the ASCII code in ascending order $ string = "jsapi_ticket = $ jsapiTicket & noncestr = $ nonceStr & timestamp = $ timestamp & url = $ url" ; $ Signature = SHA1 ($ String); $ signPackage = Array ( "for appId" => $ this-> for appId, "nonceStr" => $ nonceStr, "timestamp" => $ timestamp, "URL" => $ URL, "Signature" => $ Signature, "rawString" => $ String ); return $ signPackage; } private function createNonceStr($length = 16) { $ = chars "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $ STR = ""; for ($ I = 0; $ I <$ length; $ I ++) { . $ STR = substr ($ chars, the mt_rand (0, strlen ($ chars) -. 1 ),. 1); } return $ STR; } Private getJsApiTicket function () { // store global update should jsapi_ticket $ Redis Registry :: = GET ( 'Redis'); $ = $ redis- jsapi_ticket> GET ( 'jsapi_ticket' ); $ json_decode the Data = ($ jsapi_ticket); IF (the Data $) {! $ accessToken = $ this-> getAccessToken (); // If the enterprise is acquiring ticket using the following URL // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; $res = json_decode($this->httpGet($url)); $ticket = $res->ticket; if ($ticket) { $data['jsapi_ticket'] = $ticket; $redis->set('jsapi_ticket',json_encode($data)); $redis->expire('jsapi_ticket',7000); } } else { $ticket = $data->jsapi_ticket; } return $ticket; } private function getAccessToken() { // access_token should store and update the global $redis= Registry::get('redis'); $access_token = $redis->get('access_token'); $ json_decode the Data = ($ access_token); IF (! $ The Data) { // If the Enterprise acquisition access_token following the URL of // $ url = "HTTPS: //qyapi.weixin.qq ? .com / cgi-bin / GetToken corpid = $ this-> appId & corpsecret = $ this-> appsecret "; $ url =" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= this- $> for appId & Secret = $ this-> appsecret "; $ of json_decode RES = ($ this-> HttpGet ($ URL)); $ $ RES- the access_token => the access_token; IF (the access_token $) { $ Data [ 'the access_token' ] = $ the access_token; $ redis-> SET ( 'the access_token', json_encode ($ Data)); $redis->expire('access_token',7000); } } else { $access_token = $data->access_token; } return $access_token; } private function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } }
使用
// micro-channel configuration
$ config = (new Service_WxShare ($ appid, $ appsecret)) -> getSignPackage ();
JS:
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> <script> wx.config({ debug: false, appId: '{{ js_config.appId }}', timestamp: '{{ js_config.timestamp }}', nonceStr: '{{ js_config.nonceStr }}', signature: '{{ js_config.signature }}', jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline', 'chooseWXPay', 'showOptionMenu', "updateAppMessageShareData", "hideMenuItems", "showMenuItems", "onMenuShareTimeline", 'onMenuShareAppMessage'] }); wx.READY (function () { desc: 'Description', title: 'title', wx.onMenuShareAppMessage ({ link: 'share a link', for imgUrl: 'Image Path', Success: function () { } }); wx.onMenuShareTimeline ({ title: 'title', desc: 'Description', link: 'share a link', for imgUrl: 'image path', Success: function () { } }) }); </ Script>