php實現(xiàn)的微信分享到朋友圈并記錄分享次數(shù)功能
發(fā)布時間:2018-08-12 09:24:03
來源:
本文實例講述了php實現(xiàn)的微信分享到朋友圈并記錄分享次數(shù)功能。分享給大家供大家參考,具體如下:
1.引入JS文件
2.通過config接口注入權(quán)限驗證配置
3.通過ready接口處理成功驗證
4.通過error接口處理失敗驗證
JSDK檔說明:https://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
(1)
1
|
<script type= "text/javascript" src= "http://res.wx.qq.com/open/js/jweixin-1.0.0.js" ></script> |
(2)頁面加入獲取webconfig驗證信息的值
1
2
3
4
5
6
7
|
<?php $url =dirname(dirname(dirname(dirname(dirname(dirname(dirname( __FILE__ ))))))); $url = $url . '/addons/lb_vote/jssdk.php' ; include $url ; $jsdk = new JSSDK( 'wxa3816b432f7291ba' , 'e469db86bec9661650362dc2f9df8956' ); $signPackage = $jsdk ->GetSignPackage(); ?> |
(3)驗證config
1
2
3
4
5
6
7
8
|
wx.config({ debug: false, appId: '<?php echo $signPackage["appId"];?>' , // 必填,公眾號的唯一標識 timestamp:<?php echo $signPackage [ "timestamp" ];?>, // 必填,生成簽名的時間戳 nonceStr: '<?php echo $signPackage["nonceStr"];?>' , // 必填,生成簽名的隨機串 signature: '<?php echo $signPackage["signature"];?>' , // 必填,簽名,見附錄1 jsApiList: [ 'checkJsApi' , 'onMenuShareTimeline' ] // }); |
(4)微信分享到朋友圈接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
wx.ready( function (){ wx.onMenuShareTimeline({ title: '測試分享朋友圈功能' , // 分享標題 link: "{php echo 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];}" , // 分享鏈接 imgUrl: '{php echo $_W[' siteroot '];}{$photo}' , // 分享圖標 success: function () { // 用戶確認分享后執(zhí)行的回調(diào)函數(shù) 記錄分享的次數(shù) $.ajax({ url: "{php echo $this->createMobileUrl('Index',array('op'=>'share'))}" , type: 'post' , data: "id=" +{ $userinfo [ 'id' ]}+ "&rid=" +{ $userinfo [ 'rid' ]}, dataType: 'json' , success: function (data){ if (data.flags==1){ alert(data.msg); } else if (data.flags==2){ alert(data.msg); location.href= "{php echo $this->createMobileUrl('Index',array('op'=>'display','id'=>$id))}" rel= "external nofollow" ; } } }); }, cancel: function () { // 用戶取消分享后執(zhí)行的回調(diào)函數(shù) alert( '取消分享成功!' ); } }); }); |
(5)驗證錯誤時執(zhí)行的函數(shù)
1
2
3
|
wx.error( function (res){ alert(res); }); |
(6)PHP端更新數(shù)據(jù)庫,記錄分享次數(shù) 返回處理信息給用戶
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
if ( $op == 'share' ){ $voteinfo =pdo_fetch( "SELECT *FROM " .tablename( 'lb_vote_info' ). " WHERE rid = :rid and id=:id and uniacid=:uniacid and pass=:pass" , array ( ':rid' => $_GPC [ 'rid' ], ':uniacid' => $_W [ 'uniacid' ], ':pass' =>1, 'id' => $_GPC [ 'id' ])); $sharenum = intval ( $voteinfo [ 'sharenum' ])+1; $data = array ( 'sharenum' => $sharenum , ); $res =pdo_update( 'lb_vote_info' , $data , array ( 'id' => $_GPC [ 'id' ], 'uniacid' => $_W [ 'uniacid' ], 'rid' => $_GPC [ 'rid' ])); if (! empty ( $res )){ $msg [ 'msg' ]= '已分享到朋友圈!' ; $msg [ 'flags' ]=2; echo json_encode( $msg ); } else { $msg [ 'msg' ]= '分享失敗!' ; $msg [ 'flags' ]=1; echo json_encode( $msg ); } } |