1、微信公众号中的基本配置里面完成IP白名单配置,很重要,影响access_token
2、将下面文件下载放到目录中,只需要修改airkiss.php中的"yourAppID","yourAppSecret",其他不变
3、代码详情
1、access_token.php
<?php exit();?>
{"access_token":"","expire_time":0}
2、airkiss.php
<?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>
wx.config({
beta:true,//开启内测接口调用,注入wx.invoke方法
debug:false,//关闭调试模式
appId: '<?php echo $signPackage["appId"];?>',//AppID
timestamp: <?php echo $signPackage["timestamp"];?>,//时间戳
nonceStr: '<?php echo $signPackage["nonceStr"];?>',//随机串
signature: '<?php echo $signPackage["signature"];?>',//签名
jsApiList: [
// 所有要调用的 API 都要加到这个列表中
'configWXDeviceWiFi'
]
});
wx.ready(function () {
// 在这里调用 API
wx.checkJsApi({
jsApiList: ['configWXDeviceWiFi'],
success: function(res) {
wx.invoke('configWXDeviceWiFi', {}, function(res){
var err_msg = res.err_msg;
if(err_msg == 'configWXDeviceWiFi:ok') {
//配置成功
alert("WIFI配置成功");
window.location.href = 'https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzU5NzUzNDMwMw==&scene=124#wechat_redirect'
} else {
//配置失败
alert(err_msg);
}
});
}
});
});
wx.error(function(res){
alert("配置出错");
});
</script>
</html>
3、jsapi_ticket.php
<?php exit();?>
{"jsapi_ticket":"","expire_time":0}
4、jssdk.php
<?php
class JSSDK {
private $appId;
private $appSecret;
public function __construct($appId, $appSecret) {
$this->appId = $appId;
$this->appSecret = $appSecret;
}
public function getSignPackage() {
$jsapiTicket = $this->getJsApiTicket();
// 注意 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);
//var_dump($signature);exit;
$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 getJsApiTicket() {
// jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode($this->get_php_file("jsapi_ticket.php"));
if ($data->expire_time < time()) {
$accessToken = $this->getAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
$res = json_decode($this->httpGet($url));
$ticket = $res->ticket;
if ($ticket) {
$data->expire_time = time() + 7000;
$data->jsapi_ticket = $ticket;
$this->set_php_file("jsapi_ticket.php", json_encode($data));
}
} else {
$ticket = $data->jsapi_ticket;
}
return $ticket;
}
private function getAccessToken() {
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode($this->get_php_file("access_token.php"));
if ($data->expire_time < time()) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
//var_dump($url);exit;
$res = json_decode($this->httpGet($url));
//var_dump($res->expires_in);exit;
$access_token = $res->access_token;
//var_dump($access_token);exit;
if ($access_token) {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$this->set_php_file("access_token.php", json_encode($data));
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}
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, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
private function get_php_file($filename) {
return trim(substr(file_get_contents($filename), 15));
}
private function set_php_file($filename, $content) {
$fp = fopen($filename, "w");
fwrite($fp, "<?php exit();?>" . $content);
fclose($fp);
}
}
注意:MP_verify*.txt这个文件先不要,可以先跳过
5、找到设备功能进行开启
6、访问自己配置的目录http://aa.test.com/wx/airkiss.php便可以跳出
注意:如果此时跳出,则结束,如果还不行,可能需要JS接口安全域名配置
注意1:进入微信公众平台后台,在公众号设置->功能设置界面下配置好JS接口安全域名,如下图
此时会涉及上面下载的MP_verify*.txt文件,放到相应目录就可以,可以设置二级域名下的路径,如:aa.test.com/wx
注意2:如果需要服务器进行推送,需要创建一个index.php
<?php
define("TOKEN","echolucky");
function checkSignature()
{
//从GET参数中读取三个字段的值
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
//读取预定义的TOKEN
$token = TOKEN;
//对数组进行排序
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
//对三个字段进行sha1运算
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
//判断我方计算的结果是否和微信端计算的结果相符
//这样利用只有微信端和我方了解的token作对比,验证访问是否来自微信官方.
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
if(checkSignature()){
echo $_GET["echostr"];
}
else{
echo 'error';
}