tp3.2结合mysql实现微信自定义分享链接和图文
16lz
2021-01-22
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="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>
更多相关文章
- Android---Volley请求天气接口JSON解析
- Android调用百度地图Web端接口,实现百度定位、导航
- 避免在Java接口中使用数组的3个理由
- Java se之静态代码块、代码块、构造函数执行顺序问题
- 【Java笔记】——抽象类和接口
- C#/Java 调用WSDL接口及方法
- 关于JAVA回调函数的使用
- 第三部分:Android 应用程序接口指南---第二节:UI---第六章 对话框
- sc7731 Android 5.1 Camera 学习之二 framework 到 HAL接口整理