1.在微信公眾平臺后臺配置JS接口安全源碼

2.下載官方demo,
下載地址https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
附錄6

下載內容展示

3.在根目錄下的/Application/Home/Controller文件夾中的SdkController.class.php文件,書寫index方法
~~~
<?php
namespace Home\Controller;
use Think\Controller;
use Com\Wechat;
use Com\WechatAuth;
class SdkController extends Controller
{
private $appid="wx165112bf167af76c";
private $appSecret="c65b22bfcf03fdd98504eac299701b03";
private $WechatAuth="";//初始化WechatAuth類
private $access_token="";//緩存token
public function __construct(){
parent::__construct();//可能內部已經有這個構造方法了,因此加上這個
if(!session('token')){
$this->WechatAuth=new WechatAuth($this->appid,$this->appSecret);//初始化WechatAuth類
$WechatAuth=$this->WechatAuth;
$token=$WechatAuth->getAccessToken();
session(array('expire'=>$token['expires_in']));//設置過期時間
session('token',$token['access_token']);//緩存token
$this->access_token=$token;
}else{
$token=session('token');
$this->WechatAuth=new WechatAuth($this->appid,$this->appSecret,$token);//初始化WechatAuth類
$this->access_token=$token;//緩存token
}
}
//
public function index()
{
$this->display();
}
}
~~~
4.在根目錄下的/Application/Home文件夾中創建View文件夾,并在View文件夾中創建Sdk文件夾,并在Sdk文件夾中創建index.html文件,并將官方demo中的sample中的代碼copy到index.html文件中
~~~
<?php
require_once "jssdk.php";
$jssdk = new JSSDK("yourAppID", "yourAppSecret");
$signPackage = $jssdk->GetSignPackage();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
/*
* 注意:
* 1. 所有的JS接口只能在公眾號綁定的域名下調用,公眾號開發者需要先登錄微信公眾平臺進入“公眾號設置”的“功能設置”里填寫“JS接口安全域名”。
* 2. 如果發現在 Android 不能分享自定義內容,請到官網下載最新的包覆蓋安裝,Android 自定義分享接口需升級至 6.0.2.58 版本及以上。
* 3. 常見問題及完整 JS-SDK 文檔地址:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
*
* 開發中遇到問題詳見文檔“附錄5-常見錯誤及解決辦法”解決,如仍未能解決可通過以下渠道反饋:
* 郵箱地址:weixin-open@qq.com
* 郵件主題:【微信JS-SDK反饋】具體問題
* 郵件內容說明:用簡明的語言描述問題所在,并交代清楚遇到該問題的場景,可附上截屏圖片,微信團隊會盡快處理你的反饋。
*/
wx.config({
debug: true,
appId: '<?php echo $signPackage["appId"];?>',
timestamp: <?php echo $signPackage["timestamp"];?>,
nonceStr: '<?php echo $signPackage["nonceStr"];?>',
signature: '<?php echo $signPackage["signature"];?>',
jsApiList: [
// 所有要調用的 API 都要加到這個列表中
]
});
wx.ready(function () {
// 在這里調用 API
});
</script>
</html>
~~~
5.將官方demo中的jssdk中的部分代碼copy到SdkController.class.php文件中,并作出適當修改
~~~
<?php
namespace Home\Controller;
use Think\Controller;
use Com\Wechat;
use Com\WechatAuth;
class SdkController extends Controller
{
private $appid="wx165112bf167af76c";
private $appSecret="c65b22bfcf03fdd98504eac299701b03";
private $WechatAuth="";//初始化WechatAuth類
private $access_token="";//緩存token
private $jsapi_ticket="";//緩存jsapi_ticket
/**
* 微信api根路徑
* @var string
*/
private $apiURL = 'https://api.weixin.qq.com/cgi-bin';
public function __construct(){
parent::__construct();//可能內部已經有這個構造方法了,因此加上這個
if(!session('token')){
$this->WechatAuth=new WechatAuth($this->appid,$this->appSecret);//初始化WechatAuth類
$WechatAuth=$this->WechatAuth;
$token=$WechatAuth->getAccessToken();
session(array('expire'=>$token['expires_in']));//設置過期時間
session('token',$token['access_token']);//緩存token
$this->access_token=$token;
}else{
$token=session('token');
$this->WechatAuth=new WechatAuth($this->appid,$this->appSecret,$token);//初始化WechatAuth類
$this->access_token=$token;//緩存token
}
// jsapi_ticket 應該全局存儲與更新,以下代碼以寫入到文件中做示例
if (!session('jsapi_ticket')) {
$accessToken=$this->access_token;
// 如果是企業號用以下 URL 獲取 ticket
// $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
$res = json_decode($this->httpGet($url));
//$this->jsapi_ticket ="555";
$this->jsapi_ticket = $res->ticket;
session(array('expire'=>5));//設置過期時間,實際設置7200秒,現在設為5秒方便測試
session('jsapi_ticket', $this->jsapi_ticket);
} else {
$this->jsapi_ticket = session('jsapi_ticket');
}
}
public function test()
{
/* $accessToken = $this->access_token;
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
$res = json_decode($this->httpGet($url));*/
var_dump( $this->jsapi_ticket);
}
//
public function index()
{
$this->getSignPackage();
/* $data=$this->getSignPackage();
$this->assign('data',$data);*/
$this->display();
}
public function getSignPackage(){
echo $this->jsapi_ticket;
exit;
// 注意 URL 一定要動態獲取,不能 hardcode.
$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×tamp=$timestamp&url=$url";
$signature = sha1($string);
$signPackage = array(
"appId" => $this->appid,
"nonceStr" => $noncestr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
);
return $signPackage;
}
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;
}
private function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
// 為保證第三方服務器與微信服務器之間數據傳輸的安全性,所有微信接口采用https方式調用,必須使用下面2行代碼打開ssl安全校驗。
// 如果在部署過程中代碼在此處驗證失敗,請到 http://curl.haxx.se/ca/cacert.pem 下載新的證書判別文件。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
}
~~~
6.在微信web開發工具中測試為:

7.本節源碼下載(下載密碼:13cg)
[源碼下載](https://pan.baidu.com/s/1aw5IcLGitmqrGY5b6vZuEg)