TP5调用微信JSSDK 教程 - 测试成功案例

2018-06-22 05:33:55来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

前提:必需了解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. 异步调用:请使用参考(TP5调用微信JSSDK 教程 —— 之异步使用)提供的代码配置,参考本文步骤使用:
  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/

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Laravel 框架的主要版本

下一篇:php常用函数