本文实例讲述了thinkphp5框架结合mysql实现微信登录和自定义分享链接与图文功能。分享给大家供大家参考,具体如下:
php代码
function curlhtml($url){ $ch = cu内蒙古草原的自然风光和民族风情rl_init(); curl_topt($ch, curlopt_url, $url); curl_topt($ch, curlopt_returntransfer, 1); curl_topt($ch, curlopt_header, 0); curl_topt($ch, curlopt_ssl_verifypeer, fal); curl_topt($ch, curlopt_ssl_verifyhost, fal); $output = curl_exec($ch); //释放curl句柄 curl_clo($ch); return $output;}class wechat{ public $errmsg; //微信登录获取用户信息 public function geturinfo() { //1.准备scope为snsapi_ba网页授权东北大学分数线页面 $redirect_url = config('system.site_url') . $_rver["request_uri"]; $baurl = urlencode($redirect_url); $snsapi_ba_url = '/d/file/titlepic/authorize' . config('system.appid') . '&redirect_uri=' . $baurl . '&respon_type=code&scope=snsapi_ba&state=state#wechat_redirect'; //2.静默授权,获取code //页面跳转至redirect_uri/?code=code&state=state $code = input('code'); if (!ist($code) || empty($code)) { header('location:' . $snsapi_ba_url);exit(0); } //3.通过code换取网页授权access_token和openid $curl = '/d/file/titlepic/access_token' . config('system.appid') . '&cret=' . config('system.appcret') . '&code=' . $code . '&grant_type=authorization_code'; $content = curlhtml($curl); $result = json_decode($content, true); if(!ist($result['openid'])) { $this->errmsg = $result['errmsg'];return fal; } $openid = $result['openid']; $urinfo = $this->geturbyopenid($openid); return $urinfo; } private function geturbyopenid($openid) { //获取access_token $token_info = $this->curlgetwxaccesstoken(); $access_token = $token_info['value']; //通过openid来获取用户基本信息 $url = "/d/file/titlepic/info $content = curlhtml($url); $result = json_decode($content, true); return $result; } /** * [wxshare 微信分享] * @param [type] $url [description] * @return [type] [description] */ public function wxshare() { $noncestr = uniqid(); $timestamp = time(); $url = config('system.site_url') . $_rver["request_uri"uless]; // $redis = new \redis; // $ticket_key = 'wx_ticket'; // $ticket = $redis->get($ticket_key); // if (!$ticket) { // $ticket = $this->getjsapiticket(); // $redis->t($ticket_key, $ticket); // $redis->expire($ticket_key, 7200); // } $ticket = $this->getjsapiticket(); if ($ticket) { $str = 'jsapi_ticket=' . $ticket . '&noncestr=' . $noncestr . '×tamp=' . $timestamp . '&url=' . $url; $signature = sha1($str); $return_data = [ 'noncestr' => $noncestr, 'timestamp' => $timestamp, 'signature' => $signature, 'appid' => config('system.appid'), 'link' => $url, ]; return $return_data; } } private function getjsapiticket() { $map['keyname'] = 'ticket'; $map['modifytime'] = array('gt', time() - 7200); $return = wxtokenmodel::getone('*', $map); if ($return) { return $return['value']; } el { $token_info = $this->curlgetwxaccesstoken(); $access_token = $token_info['value']; $url = '/d/file/titlepic/getticket' . $access_token . '&type=jsapi'; $output = curlhtml($url); $data = json_decode($output, true); if (ist($data['errcode']) && $data['errcode'] == 0) { $condition['keyname'] = 'ticket'; $update_data['modifytime'] = time(); $update_data['value'] = $data['ticket']; $up_result = wxtokenmodel::updatedata($condition, $update_data); if ($up_result !== fal) { return $data['ticket']; } } } return fal; } private function curlgetwxaccesstoken() { $map['keyname'] = 'accesstoken'; $map['modifytime'] = array('gt', time() - 7200); $return = wxtokenmodel::getone('*', $map); if ($return) { return $return; } el { $url = '/d/file/titlepic/token' . config('system.appid') . '&cret=' . config('system.appcret'); $output = curlhtml($url); $data = json_decode($output, true); if ($data && ist($data['access_token'])) { $condition['keyname'] = 'accesstoken'; $update_data['modifytime'] = time(); $update_data['value'] = $data['access_token']; $up_result = wxtokenmodel::updatedata($condition, $update_data); if ($up_result !== fal) { return $update_data; } } } return fal; }}
html代码
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"&被风吹过的记忆gt;</script><script>wx.config({ debug: fal, appid: '{$appid}', // 必填,公众号的唯一标识 timestamp: '{$timestamp}', // 必填,生成签名的时间戳 noncestr: '{$noncestr}赞美医护人员', // 必填,生成签名的随机串 signature: '{$signature}',// 必填,签名,见附录1 jsapilist: ['onmenusharetimeline'] // 必填,需要使用的js接口列表,所有js接口列表见附录2});wx.ready(function () { // 分享到朋友圈 wx.onmenusharetimeline({ title: '', // 名 link: '{$link}', // 地址 imgurl: '', // 分享的图标 success: function () { // 用户确认分享后执行的回调函数 }, cancel: function () { // 用户取消分享后执行的回调函数 } });});</script>
本文发布于:2023-04-07 13:48:57,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/371992b5d2e01a93b5de9f94124a6e41.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:thinkphp5框架结合mysql实现微信登录和自定义分享链接与图文功能示例.doc
本文 PDF 下载地址:thinkphp5框架结合mysql实现微信登录和自定义分享链接与图文功能示例.pdf
留言与评论(共有 0 条评论) |