tp3.2结合mysql实现微信自定义分享链接和图文
2017-11-13 15:52
671 查看
php代码
private function generateSign()
{
$noncestr = uniqid();
$timestamp = time();
$url = 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
$ticket = $this->getJsapiTicket();
if ($ticket) {
$str = 'jsapi_ticket=' . $ticket . '&noncestr=' . $noncestr . '×tamp=' . $timestamp . '&url=' . $url;
$signature = sha1($str);
$this->assign('noncestr', $noncestr);
$this->assign('timestamp', $timestamp);
$this->assign('signature', $signature);
$this->assign('appId', 'xxxxxx');
$this->assign('link', $url);
}
}
private function getJsapiTicket()
{
$map['id'] = 1;
$map['update_time'] = array('GT', time() - 7200);
$return = M('Wx_jsapi_ticket')->where($map)->find();
if ($return) {
return $return['ticket'];
} else {
$token_info = $this->curlGetWxAccessToken();
$access_token = $token_info['access_token'];
$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $access_token . '&type=jsapi';
$output = $this->curlHtml($url);
$data = json_decode($output, true);
if ($data['errcode'] == 0) {
$condition['id'] = 1;
$data['update_time'] = time();
$data['ticket'] = $data['ticket'];
$up_result = M('Wx_jsapi_ticket')->where($condition)->save($data);
if ($up_result !== false) {
return $data['ticket'];
}
}
}
return false;
}
private function curlGetWxAccessToken()
{
$map['id'] = 1;
$map['update_time'] = array('GT', time() - 7200);
$return = M('Wx_token')->where($map)->find();
if ($return) {
return $return;
} else {
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=xxxxxx&secret=xxxxxxxxx';
$output = $this->curlHtml($url);
$data = json_decode($output, true);
if ($data['errcode'] == 0) {
$condition['id'] = 1;
$data['update_time'] = time();
$data['access_token'] = $data['access_token'];
$up_result = M('Wx_token')->where($condition)->save($data);
if ($up_result !== false) {
return $data;
}
}
}
return false;
}
private function curlHtml($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($ch);
//释放curl句柄
curl_close($ch);
return $output;
}
html代码
<script type="text/javascript" src=
4000
"http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>
wx.config({
debug: false,
appId: '<{$appId}>', // 必填,公众号的唯一标识
timestamp: <{$timestamp}>, // 必填,生成签名的时间戳
nonceStr: '<{$noncestr}>', // 必填,生成签名的随机串
signature: '<{$signature}>',// 必填,签名,见附录1
jsApiList: ['onMenuShareTimeline'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function () {
// 分享到朋友圈
wx.onMenuShareTimeline({
title: '<{$data.title}>', // 名
link: '<{$link}>', // 地址
imgUrl: '<{$data.img}>', // 分享的图标
success: function () {
// 用户确认分享后执行的回调函数
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
});
</script>
private function generateSign()
{
$noncestr = uniqid();
$timestamp = time();
$url = 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
$ticket = $this->getJsapiTicket();
if ($ticket) {
$str = 'jsapi_ticket=' . $ticket . '&noncestr=' . $noncestr . '×tamp=' . $timestamp . '&url=' . $url;
$signature = sha1($str);
$this->assign('noncestr', $noncestr);
$this->assign('timestamp', $timestamp);
$this->assign('signature', $signature);
$this->assign('appId', 'xxxxxx');
$this->assign('link', $url);
}
}
private function getJsapiTicket()
{
$map['id'] = 1;
$map['update_time'] = array('GT', time() - 7200);
$return = M('Wx_jsapi_ticket')->where($map)->find();
if ($return) {
return $return['ticket'];
} else {
$token_info = $this->curlGetWxAccessToken();
$access_token = $token_info['access_token'];
$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $access_token . '&type=jsapi';
$output = $this->curlHtml($url);
$data = json_decode($output, true);
if ($data['errcode'] == 0) {
$condition['id'] = 1;
$data['update_time'] = time();
$data['ticket'] = $data['ticket'];
$up_result = M('Wx_jsapi_ticket')->where($condition)->save($data);
if ($up_result !== false) {
return $data['ticket'];
}
}
}
return false;
}
private function curlGetWxAccessToken()
{
$map['id'] = 1;
$map['update_time'] = array('GT', time() - 7200);
$return = M('Wx_token')->where($map)->find();
if ($return) {
return $return;
} else {
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=xxxxxx&secret=xxxxxxxxx';
$output = $this->curlHtml($url);
$data = json_decode($output, true);
if ($data['errcode'] == 0) {
$condition['id'] = 1;
$data['update_time'] = time();
$data['access_token'] = $data['access_token'];
$up_result = M('Wx_token')->where($condition)->save($data);
if ($up_result !== false) {
return $data;
}
}
}
return false;
}
private function curlHtml($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$output = curl_exec($ch);
//释放curl句柄
curl_close($ch);
return $output;
}
html代码
<script type="text/javascript" src=
4000
"http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>
wx.config({
debug: false,
appId: '<{$appId}>', // 必填,公众号的唯一标识
timestamp: <{$timestamp}>, // 必填,生成签名的时间戳
nonceStr: '<{$noncestr}>', // 必填,生成签名的随机串
signature: '<{$signature}>',// 必填,签名,见附录1
jsApiList: ['onMenuShareTimeline'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function () {
// 分享到朋友圈
wx.onMenuShareTimeline({
title: '<{$data.title}>', // 名
link: '<{$link}>', // 地址
imgUrl: '<{$data.img}>', // 分享的图标
success: function () {
// 用户确认分享后执行的回调函数
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
});
</script>
相关文章推荐
- 微信自定义分享链接和图文
- 将企业协作与微信结合,明道打通微信实现从微信到明道的消息分享,目前已支持文字、图片以及链接
- 微信自定义分享链接和图文
- PHP+MySQL微信通过jsapi分享到朋友圈等平台,实现设置图片、链接。
- jssdk实现自定义分享到微信好友和微信朋友圈
- 基于thinkPHP实现的微信自定义分享功能示例
- 微信分享链接title,图片的实现(多页面)
- Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)
- 微信自定义分享的签名版实现
- PHP自定义实现微信分享朋友圈源码演示下载
- 微信页面 config注入获取权限 自定义微信分享的图片链接及标题
- 实现将网页链接分享到微信功能
- PHP自定义实现微信分享朋友圈源码演示下载
- 实现微信自定义分享网页(java)
- 微信自定义分享的标题,链接,封面
- 如何实现微信自定义分享标题、图片、描述等信息
- 微信JS-SDK实现自定义分享功能,分享给朋友,分享到朋友圈及QQ自定义分享
- 详解Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)
- 微信JS-SDK实现自定义分享功能,分享给朋友,分享到朋友圈,后台实现
- 实现微信JS-SDK分享自定义标题和图片