1. 程式人生 > >TP5調用微信JSSDK 教程

TP5調用微信JSSDK 教程

extend 圖片 raw top eve 動態獲取 namespace lose mes

前提:必需了解TP5的全局配置,擴展目錄,composer,依賴註入的概念;


第一步:首先配置加載官方JSSDK類

    "autoload": {
        "files": [],
        "classmap": [],
        "psr-4": {
            "Extend\\jssdk\\": "jssdk/"
        }
    }

執行:conposer update (註意保留鎖文件,以免更新不要的更新)

第二步:修改類如下:

技術分享圖片
  1 <?php
  2 namespace jssdk;
3 4 class Jssdk { 5 private $appId; 6 private $appSecret; 7 8 public function __construct($appId, $appSecret) { 9 $this->appId = $appId; 10 $this->appSecret = $appSecret; 11 } 12 13 public function getSignPackage() { 14 $jsapiTicket = $this
->getJsApiTicket(); 15 16 // 註意 URL 一定要動態獲取,不能 hardcode. 17 $protocol = (!empty($_SERVER[‘HTTPS‘]) && $_SERVER[‘HTTPS‘] !== ‘off‘ || $_SERVER[‘SERVER_PORT‘] == 443) ? "https://" : "http://"; 18 $url = "$protocol$_SERVER[SERVER_NAME]$_SERVER[REQUEST_URI]"; 19 20 $timestamp = time();
21 $nonceStr = $this->createNonceStr(); 22 23 // 這裏參數的順序要按照 key 值 ASCII 碼升序排序 24 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url"; 25 26 $signature = sha1($string); 27 28 $signPackage = array( 29 "appId" => $this->appId, 30 "nonceStr" => $nonceStr, 31 "timestamp" => $timestamp, 32 "url" => $url, 33 "signature" => $signature, 34 "rawString" => $string 35 ); 36 return $signPackage; 37 } 38 39 private function createNonceStr($length = 16) { 40 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 41 $str = ""; 42 for ($i = 0; $i < $length; $i++) { 43 $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); 44 } 45 return $str; 46 } 47 48 private function getJsApiTicket() { 49 // jsapi_ticket 應該全局存儲與更新,以下代碼以寫入到文件中做示例 50 $data = json_decode($this->get_php_file(EXTEND_PATH .‘jssdk‘. DS ."jsapi_ticket.php")); 51 if ($data->expire_time < time()) { 52 $accessToken = $this->getAccessToken(); 53 // 如果是企業號用以下 URL 獲取 ticket 54 // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; 55 $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; 56 $res = json_decode($this->httpGet($url)); 57 $ticket = $res->ticket; 58 if ($ticket) { 59 $data->expire_time = time() + 7000; 60 $data->jsapi_ticket = $ticket; 61 $this->set_php_file("jsapi_ticket.php", json_encode($data)); 62 } 63 } else { 64 $ticket = $data->jsapi_ticket; 65 } 66 67 return $ticket; 68 } 69 70 private function getAccessToken() { 71 // access_token 應該全局存儲與更新,以下代碼以寫入到文件中做示例 72 $data = json_decode($this->get_php_file(EXTEND_PATH .‘jssdk‘. DS ."access_token.php")); 73 if ($data->expire_time < time()) { 74 // 如果是企業號用以下URL獲取access_token 75 // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret"; 76 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; 77 $res = json_decode($this->httpGet($url)); 78 $access_token = $res->access_token; 79 if ($access_token) { 80 $data->expire_time = time() + 7000; 81 $data->access_token = $access_token; 82 $this->set_php_file("access_token.php", json_encode($data)); 83 } 84 } else { 85 $access_token = $data->access_token; 86 } 87 return $access_token; 88 } 89 90 private function httpGet($url) { 91 $curl = curl_init(); 92 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 93 curl_setopt($curl, CURLOPT_TIMEOUT, 500); 94 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 95 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 96 curl_setopt($curl, CURLOPT_URL, $url); 97 98 $res = curl_exec($curl); 99 curl_close($curl); 100 101 return $res; 102 } 103 104 private function get_php_file($filename) { 105 return trim(substr(file_get_contents($filename), 15)); 106 } 107 private function set_php_file($filename, $content) { 108 $fp = fopen($filename, "w"); 109 fwrite($fp, "<?php exit();?>" . $content); 110 fclose($fp); 111 } 112 }
View Code

第三步:調用:

  全局配置config.php;寫入APPID & APPSECRET

‘wxConfig‘=>[
‘id‘=>‘‘,
‘secret‘=>‘‘,
]
  1. 異步調用:
    use jssdk\Jssdk;

     1     /**
     2      * 微信分享接口
     3      */
     4     public function share_wx()
     5     {
     6         $config = config(‘wxConfig‘);
     7         $jssdk = new Jssdk($config[‘id‘], $config[‘secret‘]);
     8         $signPackage = $jssdk->GetSignPackage();
     9         return json([‘code‘=>1,‘msg‘=>‘success!‘,‘data‘=>$signPackage],200);
    10     }

  2. 同步調用:
    use jssdk\Jssdk;

    1     public function index()
    2     {
    3         $config = config(‘wxConfig‘);
    4         $jssdk = new Jssdk($config[‘id‘], $config[‘secret‘]);
    5         $signPackage = $jssdk->GetSignPackage();
    6         return $this->fetch(‘‘,[
    7             ‘signPackage‘=>$signPackage
    8         ]);
    9     }

第四步:完成;

資料下載包:鏈接:https://pan.baidu.com/s/1i49hovr 密碼:19ux
微信細節配置參考:http://laichuanfeng.com/work/website-wechat-share/

TP5調用微信JSSDK 教程