<?php
class share
{
private $appId;
private $appSecret;
public function __construct($appId, $appSecret)
{
$this->appId = $appId;
$this->appSecret = $appSecret;
}
public function ec()
{
$res = $this->getAccessToken();//token
$ticket = $this->getTicket($res);//ticket
$rand = $this->createNonceStr();//随机字符
$time = time();//时间戳
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; //要分享页面的url
$arr=array();
$arr['ticket'] = $ticket;
$arr['rand'] = $rand;
$arr['time'] = $time;
$arr['url'] = $url;
$string = "jsapi_ticket={$arr['ticket']}&noncestr={$arr['rand']}×tamp={$arr['time']}&url={$arr['url']}"; //签名排序
$signature = sha1($string);
$arr['signature'] = $signature;
return $arr;
}
//随机字符串
private function createNonceStr($length = 16)
{
$chars = "abcdefghijklmnopqrstuvwxyz";
$str = "";
for ($i = 0; $i < $length; $i++)
{
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
//获取 token 7200
private function getAccessToken()
{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
$data = $this->get_file('token.txt');
if(strlen($data)>1)//文件存在
{
$tokenArr = unserialize($data);
if(time()<$tokenArr['expire'])
{
goto a;
}
else
{
$tokenArr = json_decode($this->httpGet($url),true);
$tokenArr['expire'] = time()+7000;
$this->set_file('token.txt',serialize($tokenArr));
}
}
else //文件不存在
{
$tokenArr = json_decode($this->httpGet($url),true);
$tokenArr['expire'] = time()+7000;
$this->set_file('token.txt',serialize($tokenArr));
}
a:
return $tokenArr['access_token'];
}
//获取jsapi_ticket 7200
private function getTicket($res)
{
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$res."&type=jsapi";
$data = $this->get_file('ticket.txt');
if(strlen($data)>1)
{
$ticketArr = unserialize($data);
if(time() < $ticketArr['expire'])
{
goto a;
}
else
{
$ticketArr = json_decode($this->httpGet($url),true);
$ticketArr['expire'] = time()+7000;
$this->set_file('ticket.txt',serialize($ticketArr));
}
}
else
{
$ticketArr = json_decode($this->httpGet($url),true);
$ticketArr['expire'] = time()+7000;
$this->set_file('ticket.txt',serialize($ticketArr));
}
a:
return $ticketArr['ticket'];
}
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;
}
private function get_file($filename)
{
return trim(file_get_contents($filename));
}
private function set_file($filename, $content)
{
$fp = fopen($filename,"w");
fwrite($fp,$content);
fclose($fp);
}
}
html页面
<?php
$appid = 'xxxxxxxxxxxxx';
$APPSECRET = 'xxxxxxxxxxxxx';
$jssdk = new share($appid, $APPSECRET);
$signPackage = $jssdk->ec();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
</body>
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>
//alert(location.href.split('#')[0])
/*
*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 $appid; ?>',
timestamp: <?php echo $signPackage["time"]; ?>,
nonceStr: '<?php echo $signPackage["rand"]; ?>',
signature: '<?php echo $signPackage["signature"]; ?>',
jsApiList: [
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'hideMenuItems',
'showMenuItems',
'hideAllNonBaseMenuItem',
'showAllNonBaseMenuItem',
'translateVoice',
'startRecord',
'stopRecord',
'onRecordEnd',
'playVoice',
'pauseVoice',
'stopVoice',
'uploadVoice',
'downloadVoice',
'chooseImage',
'previewImage',
'uploadImage',
'downloadImage',
'getNetworkType',
'openLocation',
'getLocation',
'hideOptionMenu',
'showOptionMenu',
'closeWindow',
'scanQRCode',
'chooseWXPay',
'openProductSpecificView',
'addCard',
'chooseCard',
'openCard'
]
});
wx.ready(function ()
{
wx.onMenuShareTimeline({
title: 'xxxxxxxxxxx', // 分享标题
link: 'xxxxxxxxxxxxx',
imgUrl: 'http://www.lanrenmb.com/templets/lanrenmb/img/logo.png', // 分享图标
success: function () {
// 用户确认分享后执行的回调函数
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
/* var shareData = {
title: '标题',
desc: '描述',
link: 'xxxxxxxxxx',//链接
imgUrl: 'xxxxxxxxxxxx'//图片地址
};
wx.onMenuShareAppMessage(shareData);
wx.onMenuShareTimeline(shareData);
wx.onMenuShareQQ(shareData);
wx.onMenuShareWeibo(shareData);*/
});
wx.error(function (res)
{
console.log(res.errMsg);
});
</script>
</html>
大概就是这样了
网友评论