TP5 微信分享朋友圈接口顯示自定義圖片和標題

在tp5框架下對接微信分享朋友圈接口,並顯示圖片和標題。

要使用 微信JS-SDK 進行調用;

準備步驟:

1,先登錄微信公衆平臺進入“公衆號設置”的“功能設置”裏填寫“JS接口安全域名”。

2,獲取公衆號對應的 appid 和 appsecret。

3.公衆號取得認證。

不羅嗦,直接上代碼:

1,js 代碼,在需要分享的頁面添加:

<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>//引入js
<script>
window.onload=function(){
var ajaxurl =  '{:url("index/weixin")}';
var urll=location.href.split('#')[0];
$.ajax({
    url:ajaxurl,
    type:"post",
    data:{urll:urll},
    dataType:"json",
    success:function(s){
        wx.config({
            debug: true, //分享成功後可以關閉 false
            appId: s.appid,
            timestamp: s.timestamp,
            nonceStr: s.nonceStr,
            signature: s.signature,
            jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage']
        });
        wx.ready(function(){
            wx.onMenuShareTimeline({
                title: '{$subtitle}', // 分享標題
                link: s.url, // 分享鏈接,該鏈接域名或路徑必須與當前頁面對應的公衆號JS安全域名一致
                imgUrl: "imgurl", // 分享圖標 使用絕對路徑
                success: function () {

                }
            });
            wx.onMenuShareAppMessage({
                title: '{$subtitle}',
                desc: s.url, // 分享描述
                link: s.url, // 分享鏈接,該鏈接域名或路徑必須與當前頁面對應的公衆號JS安全域名一致
                imgUrl: "imgurl", // 分享圖標 使用絕對路徑
                type: '', // 分享類型,music、video或link,不填默認爲link
                dataUrl: '', // 如果type是music或video,則要提供數據鏈接,默認爲空
                success: function () {
                
                }
            });
        });
        
    },
    error:function(){
        console.log("通信失敗");
    }
});
}
/* alert(location.href.split('#')[0]); */ //彈出的url必須與訪問地址一致

</script>

2.php代碼

public function weixin(){
        $url = input('urll');//獲取當前頁面的url,接收請求參數
        $root['url'] = $url;
        //獲取access_token,並緩存
        $file = RUNTIME_PATH.'/access_token';//緩存文件名access_token
        $appid='xxxxxxxxxxx'; // 填寫自己的appid
        $secret='xxxxxxxxxxx'; // 填寫自己的appsecret
        $expires = 3600;//緩存時間1個小時
        if(file_exists($file)) {
        $time = filemtime($file);
        if(time() - $time > $expires) {
        $token = null;
        }else {
        $token = file_get_contents($file);
        }
        }else{
        fopen("$file", "w+");
        $token = null;
        }
        if (!$token || strlen($token) < 6) {
        $res = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."");
        $res = json_decode($res, true);
        $token = $res['access_token'];
        // write('access_token', $token, 3600);
        @file_put_contents($file, $token);
        }

        //獲取jsapi_ticket,並緩存
        $file1 = RUNTIME_PATH.'/jsapi_ticket';
        if(file_exists($file1)) {
        $time = filemtime($file1);
        if(time() - $time > $expires) {
        $jsapi_ticket = null;
        }else {
        $jsapi_ticket = file_get_contents($file1);
        }
        }else{
        fopen("$file1", "w+");
        $jsapi_ticket = null;
        }
        if (!$jsapi_ticket || strlen($jsapi_ticket) < 6) {
        $ur = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$token&type=jsapi";
        $res = file_get_contents($ur);
        $res = json_decode($res, true);
        $jsapi_ticket = $res['ticket'];
        @file_put_contents($file1, $jsapi_ticket);
        }

        $timestamp = time();//生成簽名的時間戳
        $metas = range(0, 9);
        $metas = array_merge($metas, range('A', 'Z'));
        $metas = array_merge($metas, range('a', 'z'));
        $nonceStr = '';
        for ($i=0; $i < 16; $i++) {
        $nonceStr .= $metas[rand(0, count($metas)-1)];//生成簽名的隨機串
        }

        $string1="jsapi_ticket=".$jsapi_ticket."&noncestr=".$nonceStr."&timestamp=".$timestamp."&url=".$url."";
        $signature=sha1($string1);
        $root['appid'] = $appid;
        $root['nonceStr'] = $nonceStr;
        $root['timestamp'] = $timestamp;
        $root['signature'] = $signature;

        echo json_encode($root);
    }

可能遇到的問題:

調用一切正常,頁面彈出 {errMsg: config:ok} 但是分享出去不是自己定義好的圖片和標題,很可能是公衆號沒認證,查看一下接口權限。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章