公众号转发分享
<?php
include_once "jssdk.php";
$appid = "wx3f8e5c01b8f7a6b7";
$secret = "d5b33d0968ef9e4aaf8a29541cab68f9";
$jssdk = new JSSDK($appid, $secret);
$signPackage = $jssdk->GetSignPackage();
$code = $_GET["code"];
if(!empty($code))
{
$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = getJson($oauth2Url);
// 获得 access_token 和openid
$access_token = $oauth2["access_token"];
$openid = $oauth2['openid'];
$get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
$userinfo = getJson($get_user_info_url);
if($userinfo)
{
if(empty($userinfo["openid"]))
{
header("Location:index.php");
}
$user=$GLOBALS["db"]->getRow("select * from `user` where openid = '".$userinfo["openid"]."'");
if(empty($user))
{
$name= $userinfo["nickname"];
$face= $userinfo["headimgurl"];
$oppenid= $userinfo["openid"];
$result = $GLOBALS["db"]->query("insert into `user` (name,face,openid) values ('".$name."','".$face."','".$openid."')");
if($result)
{
$use=$GLOBALS["db"]->getRow("select * from `user` where openid = '".$userinfo["openid"]."'");
$_SESSION["uid"]=$use['id'];
$_SESSION["openid"]=$use['openid'];
}
}
else
{
$_SESSION["uid"]=$user['id'];
$_SESSION["openid"]=$user['openid'];
}
}
else
{
header("Location:index.php");
}
}
function getJson($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>测试分享</title>
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<script src="/index/js/jquery-1.11.1.js" language="javascript"></script>
</head>
<body style="background:#f4f7fe;">
<div class="allbox">
测试分享
</div>
<script type="text/javascript">
var link = 'http://xxxxx.com';
var id = '<?php echo $signPackage["appId"]; ?>';//服务端设置的id,用于下面拼接生成需要分享的link
var timestamp = '<?php echo $signPackage["timestamp"]; ?>';//因为服务端是String类型,此处转化成数值类型
var nonceStr = '<?php echo $signPackage["nonceStr"]; ?>';
var signature ='<?php echo $signPackage["signature"]; ?>';
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: id, // 必填,公众号的唯一标识
timestamp: timestamp, // 必填,生成签名的时间戳
nonceStr: nonceStr, // 必填,生成签名的随机串
signature: signature,// 必填,签名,见附录1
jsApiList: [
"checkJsApi",
"updateAppMessageShareData",
"updateTimelineShareData"
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function (){
let url=window.location.search
wx.checkJsApi({
jsApiList: ['updateAppMessageShareData,updateTimelineShareData'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
success: function(res) {
console.log(res)
}
});
wx.updateAppMessageShareData({
title: '<?php echo $global_title; ?>', // 分享标题
desc: '抢电器、抢马桶等更多优惠等你来,一键领取', // 分享描述
link: 'http://mall.elss.com.cn', // 分享链接
imgUrl: 'http://mall.elss.com.cn/index/images/20221117102837.jpg', // 分享图标
success: function () {
// 设置成功
}
})
wx.updateTimelineShareData({
title: '<?php echo $global_title; ?>', // 分享标题
link: 'http://mall.elss.com.cn', // 分享链接
imgUrl: 'http://mall.elss.com.cn/index/images/20221117102837.jpg', // 分享图标
success: function () {
// 设置成功
}
})
});
wx.error(function(res){
})
</script>
</body>
</html>
微信SDK:jssdk.php
原创文章,转载请注明:公众号转发分享 | 知识改变命运