微信开发之SDK

<?php
class JSSDK {
private $appId;//appid
private $appSecret;//应用密钥
// echo $appId;
// echo $appSecret;
public function __construct($appId, $appSecret) {
$this->appId = $appId;
$this->appSecret = $appSecret;
}

public function getSignPackage() {
$jsapiTicket = $this->getJsApiTicket();
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$timestamp = time();
$nonceStr = $this->createNonceStr();

// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$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;
}

public function getJsApiTicket() {
// jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
$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;
return $ticket;
}

public function getAccessToken() {
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
$res = json_decode($this->httpGet($url));
$access_token = $res->access_token;
return $access_token;
}
function httpGet($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
// curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$temp = curl_exec($ch);
curl_close($ch);
return $temp;

}

}
?>

<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<?php
$jssdk = new JSSDK("you APPID", "you appSecret");
$signPackage = $jssdk->getSignPackage();
?>
<script>
// 注意:所有的JS接口只能在公众号绑定的域名下调用,公众号开发者需要先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。
// 如果发现在 Android 不能分享自定义内容,请到官网下载最新的包覆盖安装,Android 自定义分享接口需升级至 6.0.2.58 版本及以上。
// 完整 JS-SDK 文档地址:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
//https://mp.weixin.qq.com/wiki?action=doc&id=mp1421141115&t=0.5189975742348003#dlwz1

wx.config({
debug: true,//是否调试模式
appId: 'APPid',//切记更换APPID
timestamp: <?php echo $signPackage["timestamp"];?>,//必填,生成签名的时间戳
nonceStr: '<?php echo $signPackage["nonceStr"];?>',// 必填,生成签名的随机串
signature: '<?php echo $signPackage["signature"];?>',// 必填,签名
jsApiList: ['openLocation'] // 必填,需要使用的JS接口列表,多个之间用逗号隔开
});
wx.ready(function () {
wx.getLocation({
type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
success: function (res) {

    var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90

    var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。

    var speed = res.speed; // 速度,以米/每秒计

    var accuracy = res.accuracy; // 位置精度
    alert(latitude);
    alert(longitude);
    
  }

});

});
</script>

有不足之处还望提出。

推荐阅读更多精彩内容