tp3.2结合mysql实现微信自定义分享链接和图文

来源:互联网 发布:catti含金量 知乎 编辑:程序博客网 时间:2024/06/04 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>


阅读全文
0 0