溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

微信sdk實現(xiàn)禁止微信分享(使用原生php實現(xiàn))

發(fā)布時間:2020-10-13 18:52:19 來源:腳本之家 閱讀:205 作者:huaweichenai 欄目:web開發(fā)

在此之前我們已經(jīng)學(xué)習(xí)過微信的sdk使用,但是之前實在easyWechat的php插件基礎(chǔ)上實現(xiàn)的,具體可以參考:https://www.jb51.net/article/174309.htm

這里我們來使用原生的php實現(xiàn)微信的sdk-禁止微信分享

一:引入所需要的js

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>

二:通過config接口注入權(quán)限驗證配置

wx.config({
 debug: true, // 開啟調(diào)試模式,調(diào)用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數(shù),可以在pc端打開,參數(shù)信息會通過log打出,僅在pc端時才會打印。
 appId: '', // 必填,公眾號的唯一標(biāo)識
 timestamp: , // 必填,生成簽名的時間戳
 nonceStr: '', // 必填,生成簽名的隨機串
 signature: '',// 必填,簽名
 jsApiList: [] // 必填,需要使用的JS接口列表
});

簽名的生成步驟如下:

1:獲取微信公眾號的全局唯一接口調(diào)用憑據(jù)access_token

調(diào)取下面的接口獲取access_token

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

get參數(shù)說明:

appid:微信公眾號的唯一標(biāo)識appId

secret:微信公眾號的appsecret

根據(jù)上面的接口可以獲取到微信公眾號的全局唯一接口調(diào)用憑據(jù)access_token,接口返回結(jié)果如下:

{"access_token":"ACCESS_TOKEN","expires_in":7200}

2:根據(jù)access_token獲取jsapi_ticket

調(diào)取下面的接口獲取jsapi_ticket

https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi

get參數(shù)說明:

access_token:上面接口獲取到的access_token

type:類型,這里指定填jsapi

根據(jù)上面的接口可以獲取到j(luò)sapi_ticket,接口返回結(jié)果如下:

{
 "errcode":0,
 "errmsg":"ok",
 "ticket":"bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA",
 "expires_in":7200
}

3:根據(jù)獲取到的jsapi_ticket和noncestr,timestamp,url生成簽名

對所有待簽名參數(shù)按照字段名的ASCII 碼從小到大排序后,使用URL鍵值對的格式(即key1=value1&key2=value2…)拼接成字符串,然后通過sha1加密生成簽名

三:通過ready接口處理成功驗證后在ready內(nèi)寫我們所需要的微信sdk接口

wx.ready(function(){
 // config信息驗證后會執(zhí)行ready方法,所有接口調(diào)用都必須在config接口獲得結(jié)果之后,config是一個客戶端的異步操作,所以如果需要在頁面加載時就調(diào)用相關(guān)接口,則須把相關(guān)接口放在ready函數(shù)中調(diào)用來確保正確執(zhí)行。對于用戶觸發(fā)時才調(diào)用的接口,則可以直接調(diào)用,不需要放在ready函數(shù)中。
});

四:具體實現(xiàn)

根據(jù)如上說明,這里我們就以禁止微信分享sdk為例實現(xiàn):

1:php端:

public function actionTicket()
{
  //開啟session
  session_start();
  //appId
  $appId = 'wx73d0c47a64aa5315';
  //secret
  $appSecret = 'aba2793c10623350f6aeee5a728099d3';
  if (!isset($_SESSION['ticket'])){
    //獲取微信公眾號全局唯一接口調(diào)用憑據(jù)access_token
    $result = $this->getAccessToken($appId, $appSecret);
    $accessToken = $result['access_token'];
    //獲取jsapi_ticket
    $getTicket = $this->getTicket($accessToken);
    $ticket = $getTicket['ticket'];
    $_SESSION['ticket'] = $ticket;
  }
  //jsapi_ticket(公眾號用于調(diào)用微信JS接口的臨時票據(jù))
  $ticket = $_SESSION['ticket'];
  //當(dāng)前時間戳
  $timestamp = time();
  //隨機字符串
  $noncestr = $this->getRandCode();
  //當(dāng)前url
  $url = $this->getUrl();
  $params = [
    'jsapi_ticket' => $ticket,
    'timestamp' => $timestamp,
    'noncestr' => $noncestr,
    'url' => $url,
  ];
  $options = $this->urlParams($params);
  //獲取簽名
  $signature = sha1($options);
  //將appId,timestamp,noncestr,signature渲染到頁面
  return $this->render('ticket', [
    'appId' => $appId,
    'timestamp' => $timestamp,
    'noncestr' => $noncestr,
    'signature' => $signature,
  ]);
}
/*
 * 獲取jsapi_ticket
 */
public function getTicket($accessToken)
{
  $params = [
    'access_token' => $accessToken,
    'type' => 'jsapi',
  ];
  $urlParams = $this->urlParams($params);
  $ticketUrl = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?' . $urlParams;
  $result = $this->http_curl($ticketUrl);
  return json_decode($result, true);
}
/*
 * 獲取微信公眾號的全局唯一接口調(diào)用憑據(jù)access_token
 */
public function getAccessToken($appId, $appSecret)
{
  $params = [
    'grant_type' => 'client_credential',
    'appid' => $appId,
    'secret' => $appSecret,
  ];
  $urlParams = $this->urlParams($params);
  $accessUrl = 'https://api.weixin.qq.com/cgi-bin/token?' . $urlParams;
  $result = $this->http_curl($accessUrl);
  return json_decode($result, true);
}
/*
 * 獲取隨機碼
 */
function getRandCode($num = 16){
  $array = array(
    'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
    'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','v','z',
    '1','2','3','4','5','6','7','8','9','0',
  );
  $tmpstr='';
  $max = count($array);
  for($i=1; $i<=$num; $i++){
    $key = rand(0,$max-1); //'A' -> $array[0]
    $tmpstr .= $array[$key];
  }
  return $tmpstr;
}
/*
 * curl接口調(diào)用
 */
public function http_curl($url, $data=null) {
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  $result = curl_exec($curl);
  curl_close($curl);
  return $result;
}
/*
 * 字符串拼接
 */
public function urlParams($params)
{
  ksort($params);
  reset($params);
  $options = '';
  foreach ($params as $key => $value) {
    $options .= $key . '=' . $value .'&';
  }
  $options = rtrim($options, '&');
  return $options;
}
/*
 * 獲取當(dāng)前url
 */
public function getUrl()
{
  //獲取協(xié)議類型
  $protocalPort = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
  //獲取當(dāng)前執(zhí)行腳本的url
  $phpSelf = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
  $pathInfo = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
  $queryString = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '';
  $relateUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $phpSelf . (!empty($queryString) ? '?' . $queryString : $pathInfo);
  $url = $protocalPort . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') . $relateUrl;
  return $url;
}

2:前端:

<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script>
  $(document).ready(function(){
    wx.config({
      debug: false, // 開啟調(diào)試模式為true后可以通過alert彈窗將公眾號簽名等結(jié)果反饋出來
      appId: '<?= $appId ?>', // 必填,公眾號的唯一標(biāo)識
      timestamp: '<?= $timestamp ?>', // 必填,生成簽名的時間戳
      nonceStr: '<?= $noncestr ?>', // 必填,生成簽名的隨機串
      signature: '<?= $signature ?>',// 必填,簽名
      jsApiList: [
        "hideOptionMenu",
      ]
    });
    //配置成功以后config:ok
    wx.ready(function () {
      //隱藏右上角菜單接口
      wx.hideOptionMenu();
    })
  })
</script>

這樣我們就是實現(xiàn)了原生php使用微信sdk

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI