php实现的微信分享到朋友圈并记录分享次数功能
本文实例讲述了php实现的微信分享到朋友圈并记录分享次数功能。分享给大家供大家参考,具体如下:
1.引入JS文件
2.通过config接口注入权限验证配置
3.通过ready接口处理成功验证
4.通过error接口处理失败验证
JSDK档说明:https://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
(1)
(2)页面加入获取webconfig验证信息的值
GetSignPackage(); ?>
(3)验证config
wx.config({
debug:false,
appId:'',//必填,公众号的唯一标识
timestamp:,//必填,生成签名的时间戳
nonceStr:'',//必填,生成签名的随机串
signature:'',//必填,签名,见附录1
jsApiList:['checkJsApi','onMenuShareTimeline']//
});
(4)微信分享到朋友圈接口
wx.ready(function(){
wx.onMenuShareTimeline({
title:'测试分享朋友圈功能',//分享标题
link:"{phpecho'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];}",//分享链接
imgUrl:'{phpecho$_W['siteroot'];}{$photo}',//分享图标
success:function(){
//用户确认分享后执行的回调函数记录分享的次数
$.ajax({
url:"{phpecho$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);
}elseif(data.flags==2){
alert(data.msg);
location.href="{phpecho$this->createMobileUrl('Index',array('op'=>'display','id'=>$id))}"rel="externalnofollow";
}
}
});
},
cancel:function(){
//用户取消分享后执行的回调函数
alert('取消分享成功!');
}
});
});
(5)验证错误时执行的函数
wx.error(function(res){
alert(res);
});
(6)PHP端更新数据库,记录分享次数返回处理信息给用户
if($op=='share'){
$voteinfo=pdo_fetch("SELECT*FROM".tablename('lb_vote_info')."WHERErid=:ridandid=:idanduniacid=:uniacidandpass=: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;
echojson_encode($msg);
}else{
$msg['msg']='分享失败!';
$msg['flags']=1;
echojson_encode($msg);
}
}
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP微信开发技巧汇总》、《PHP编码与转码操作技巧汇总》、《PHP网络编程技巧总结》、《php字符串(string)用法总结》、《PHP中json格式数据操作技巧汇总》及《PHP针对XML文件操作技巧总结》
希望本文所述对大家PHP程序设计有所帮助。