php頁面調用微信掃一掃

  1. functions.php
  2. <?php
  3. define("appID", "微信公衆號appId");
  4. define("appsecret", "");
  5.  
  6. class JSSDK {
  7.   private $appId;
  8.   private $appSecret;
  9.  
  10.   public function __construct($appId, $appSecret) {
  11.     $this->appId = $appId;
  12.     $this->appSecret = $appSecret;
  13.   }
  14.  
  15.   public function getSignPackage() {
  16.     $jsapiTicket = $this->getJsApiTicket();
  17.     $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  18.     $timestamp = time();
  19.     $nonceStr = $this->createNonceStr();
  20.  
  21.     $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
  22.  
  23.     $signature = sha1($string);
  24.  
  25.     $signPackage = array(
  26.       "appId"     => $this->appId,
  27.       "nonceStr"  => $nonceStr,
  28.       "timestamp" => $timestamp,
  29.       "url"       => $url,
  30.       "signature" => $signature,
  31.       "rawString" => $string
  32.     );
  33.     return $signPackage; 
  34.   }
  35.  
  36.   private function createNonceStr($length = 16) {
  37.     $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  38.     $str = "";
  39.     for ($i = 0; $i < $length; $i++) {
  40.       $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  41.     }
  42.     return $str;
  43.   }
  44.  
  45.   private function getJsApiTicket() {
  46.     $data = json_decode(file_get_contents("wp-token/jsapi_ticket.json"));
  47.     if ($data->expire_time < time()) {
  48.       $accessToken = $this->getAccessToken();
  49.       $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
  50.       $res = json_decode($this->httpGet($url));
  51.       $ticket = $res->ticket;
  52.       if ($ticket) {
  53.         $data->expire_time = time() + 7000;
  54.         $data->jsapi_ticket = $ticket;
  55.         $fp = fopen("wp-token/jsapi_ticket.json", "w");
  56.         fwrite($fp, json_encode($data));
  57.         fclose($fp);
  58.       }
  59.     } else {
  60.       $ticket = $data->jsapi_ticket;
  61.     }
  62.  
  63.     return $ticket;
  64.   }
  65.  
  66.   public function getAccessToken() {
  67.     $data = json_decode(file_get_contents("wp-token/access_token.json"));
  68.     if ($data->expire_time < time()) {
  69.       $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
  70.       $res = json_decode($this->httpGet($url));
  71.       $access_token = $res->access_token;
  72.       if ($access_token) {
  73.         $data->expire_time = time() + 7000;
  74.         $data->access_token = $access_token;
  75.         $fp = fopen("wp-token/access_token.json", "w");
  76.         fwrite($fp, json_encode($data));
  77.         fclose($fp);
  78.       }
  79.     } else {
  80.       $access_token = $data->access_token;
  81.     }
  82.     return $access_token;
  83.   }
  84.  
  85.   public function httpGet($url) {
  86.     $curl = curl_init();
  87.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  88.     curl_setopt($curl, CURLOPT_TIMEOUT, 500);
  89.     curl_setopt($curl, CURLOPT_URL, $url);
  90.  
  91.     $res = curl_exec($curl);
  92.     curl_close($curl);
  93.  
  94.     return $res;
  95.   }
  96. }
  97. ?>
  98. --------------------- 
  99. <div class="blockcode"><blockquote><?php
  100. require_once("functions.php");
  101. $jssdk=new JSSDK(appID,appsecret);
  102. $signPackage = $jssdk->GetSignPackage();
  103. require_once('header.php');
  104. ?>
  105. <a href="javascript:;">掃一掃</a>
  106. <?php
  107. require_once('footer.php');
  108. ?>
  109. <script src="scripts/jquery/3.1.1/jquery.min.js"></script>
  110. <script src="scripts/jweixin-1.0.0.js"></script>
  111. <script>
  112.   wx.config({
  113.     debug: false,
  114.     appId: '<?php echo $signPackage["appId"];?>',
  115.     timestamp: <?php echo $signPackage["timestamp"];?>,
  116.     nonceStr: '<?php echo $signPackage["nonceStr"];?>',
  117.     signature: '<?php echo $signPackage["signature"];?>',
  118.     jsApiList: [
  119.       // 所有要調用的 API 都要加到這個列表中
  120.           "scanQRCode"
  121.     ]
  122.   });
  123.   wx.ready(function () {
  124.     // 在這裏調用 API
  125.         $(document).on("click","a",function(){
  126.                 wx.scanQRCode({
  127.                         needResult: 1, // 默認爲0,掃描結果由微信處理,1則直接返回掃描結果,
  128.                         scanType: ["qrCode","barCode"], // 可以指定掃二維碼還是一維碼,默認二者都有
  129.                         success: function (res) {
  130.                                 //alert(res);
  131.                                 for(i in res ){
  132.                                 //  alert(i);           //獲得屬性 
  133.                                   alert(i + "---" + res[i]);  //獲得屬性值
  134.                                 }
  135.                                 //var result = res.resultStr; // 當needResult 爲 1 時,掃碼返回的結果
  136.                         }
  137.                 });
  138.  
  139.         })
  140.   });
  141. </script>
  142. --------------------- 
  143.  
  144.  

複製代碼

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章