## php實現微信掃一掃功能
scan.php
```
<?php
namespace app\index\controller;
class extends Controller
{
public function wxScan()
{
$url = input('param.xurl'); //需要調起掃一掃頁面的完整路徑
$appID = ''; //公眾號appid ,公眾號開發配置處可查看
$appsecret = ''; //公眾號appi
$res = new JSSDK($appID,$appsecret);
$data = $res->getWxConfig($url);
return json(['data'=>$data]);
}
}
```
JSSDK.php
```
<?php
namespace app\index\model;
use think\Model;
class JSSDK extends Model
{
public $appId;
private $appSecret;
/**
* @name 初始化參數
* @author cq <just_leaf@foxmail.com>
* @copyright zydbbt 2018-10-27
*/
public function __construct( $appId , $appSecret ) {
$this->appId = $appId;
$this->appSecret = $appSecret;
}
/**
* @name 獲取accessToken
* @author cq <just_leaf@foxmail.com>
* @copyright zydbbt 2018-10-27
*/
public function getAcc(){
return $this -> getAccessToken();
}
/**
* @name 獲取config接口注入權限驗證配置
* @author cq <just_leaf@foxmail.com>
* @copyright zydbbt 2018-10-27
*/
public function getWxConfig($url) {
# - 獲取 jsapi_ticket
$jsapiTicket = $this -> getJsApiTicket();
# - 獲取調用頁面的url
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
# - 時間戳
$timestamp = time();
# - 獲取隨機字符串
$nonceStr = $this -> createNonceStr();
# - 這里參數的順序要按照 key 值 ASCII 碼升序排序
# - 亦可把參數以數組存值,ksort() - 以升序對關聯數組進行排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
# - sha1獲取簽名
$signature = sha1($string);
# - 頁面所需注入參數
$WxConfig = array(
"appId" => $this -> appId,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
);
# - 返回
return $WxConfig;
}
/**
* @name 獲取JsApiTicket
* @author cq <just_leaf@foxmail.com>
* @copyright zydbbt 2018-10-27
*/
private function getJsApiTicket() {
# - 判斷緩存
$ticket = Session('ticket');
if(!$ticket) {
# - 獲取
$accessToken = $this->getAccessToken();
# - 獲取Ticket
# - 如果是企業號用以下 URL 獲取 ticket
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
# - get請求,轉換數組
$result = json_decode($this->httpGet($url),true);
$ticket = $result['ticket'];
# - 全局緩存
if ($ticket) {
# - 官方返回
# - {
# - "errcode":0,
# - "errmsg":"ok",
# - "ticket":"bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA",
# - "expires_in":7200
# - }
Session('ticket',$ticket,$result['expires_in']);
}
}
# - 返回
return $ticket;
}
/**
* @name 獲取AccessToken
* @author cq <just_leaf@foxmail.com>
* @copyright zydbbt 2018-10-27
*/
private function getAccessToken() {
# - 判斷緩存
$access_token = Session('accesToken');
if(!$access_token) {
# - 如果是企業號用以下URL獲取access_token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
# - get請求,轉換數組
$result = json_decode($this->httpGet($url),true);
$access_token = $result['access_token'];
# - 全局緩存
if ($access_token) {
Session('accesToken',$result['access_token'],$result['expires_in']);
}
}
# - 返回
return $access_token;
}
/**
* @name GET請求
* @author cq <just_leaf@foxmail.com>
* @copyright zydbbt 2018-10-27
*/
private function httpGet($url) {
# - 初始化
$curl = curl_init();
# - 為保證第三方服務器與微信服務器之間數據傳輸的安全性,所有微信接口采用https方式調用,必須使用下面2行代碼打開ssl安全校驗。
# - 如果在部署過程中代碼在此處驗證失敗,請到 http://curl.haxx.se/ca/cacert.pem 下載新的證書判別文件。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_URL, $url);
# - 請求
$res = curl_exec($curl);
# - 關閉
curl_close($curl);
# - 返回
return $res;
}
/**
* @name 產生隨機字符串
* @author cq <just_leaf@foxmail.com>
* @copyright zydbbt 2018-10-27
*/
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;
}
}
```