1. 程式人生 > >微信封裝的呼叫微信簽名包的類庫

微信封裝的呼叫微信簽名包的類庫

<?php
namespace Home\Model;
use Think\Model;
class WechatModel extends Model {
private $_token = ''; //令牌
       private $appid;
       private $appsecret;
    public function __construct()
    {
        $this->appid = C('APPID');//公眾號的appid
        $this->appsecret = C('APPSECRET');//公眾號的祕鑰
    }
    //呼叫js-sdk的簽名包
    public function getSignPackage() {
   $jsapiTicket = $this->getJsApiTicket();
   // 注意 URL 一定要動態獲取,不能 hardcode.(獲取當前網頁的url)
   $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();
   // 這裡引數的順序要按照 key 值 ASCII 碼升序排序
   $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
   //生成字串是用來簽名用的
   $signature = sha1($string);
   $signPackage = array(
     "appId"     => $this->appid,
     "nonceStr"  => $nonceStr,
     "timestamp" => $timestamp,
     "url"       => $url,
     "signature" => $signature,
     "rawString" => $string
   );
   return $signPackage; 
  }
  //使用會員卡領取的簽名包
  public function getHuiYuanSignPackage() {
   $apiTicket = $this->getApiTicket();
   // 注意 URL 一定要動態獲取,不能 hardcode.(獲取當前網頁的url)
   $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();
   // 這裡引數的順序要按照 key 值 ASCII 碼升序排序
   $string = $timestamp.$apiTicket."car_id";//card_id為自己建立的會員卡的id
   //生成字串是用來簽名用的
   $signature = sha1($string);
   $signPackage = array(
     "timestamp" => $timestamp,
     "signature" => $signature,
   );
   return $signPackage; 
  }
  //獲取會員卡的api_ticket
  public function getApiTicket(){
  $data = json_decode(file_get_contents("api_ticket.json"));
    if ($data->expire_time < time()) {
      $accessToken = $this->getAccessToken();
      $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card&access_token=$accessToken";
      $res = json_decode($this->httpGet($url));
      $ticket = $res->ticket;
      if ($ticket) {
        $data->expire_time = time() + 7000;
        $data->jsapi_ticket = $ticket;
        $fp = fopen("api_ticket.json", "w");
        fwrite($fp, json_encode($data));
        fclose($fp);
      }
    } else {
      $ticket = $data->jsapi_ticket;
    }
    return $ticket;
  }
  //獲取隨機字串
  private function createNonceStr($length = 16) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $str = "";
    for ($i = 0; $i < $length; $i++) {
      $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
  }
    //獲取Access Token
    public function getAccessToken(){
    //將json字串轉換為json物件(json_encode是將陣列轉換為json字串,json_decode("",true) 如果加true是將json字串轉化為php陣列,不加true轉換為PHP物件)
   $data = json_decode(file_get_contents("access_token.json"));
   if ($data->expire_time < time()) {
     // 如果是企業號用以下URL獲取access_token