新版H5微信网页自定义分享功能实现

2021-08-30   阅读:3078   分类:后端    标签: 微信开发

最近在做H5微信微信分享功能,用的是1.0的版本,发现分享功能不能用了,原因是微信已经关闭之前的分享接口,只需用回新的分享给好友updateAppMessageShareData和分享到朋友圈updateTimelineShareData即可

新的接口分享使用方法:

1、PHP代码:appid和secret、生成签名的随机串、获取access_token、获取ticket、生成wx.config需要的参数

<?php
// 步骤1.appid和secret
header("Access-Control-Allow-Origin:*");
$appid = "appid";  
$secret = "secret";  

// 步骤2.生成签名的随机串
function nonceStr($length){
	$str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJK1NGJBQRSTUVWXYZ';//随即串,62个字符
	$strlen = 62;
	while($length > $strlen){
		$str .= $str;
		$strlen += 62;
	}
	$str = str_shuffle($str);
	return substr($str,0,$length);
}
// 步骤3.获取access_token
$result = http_get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret);
$json = json_decode($result,true);
$access_token = $json['access_token'];
function http_get($url){
	$oCurl = curl_init();
	if(stripos($url,"https://")!==FALSE){
		curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
	}
	curl_setopt($oCurl, CURLOPT_URL, $url);
	curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
	$sContent = curl_exec($oCurl);
	$aStatus = curl_getinfo($oCurl);
	curl_close($oCurl);
	if(intval($aStatus["http_code"])==200){
		return $sContent;
	}else{
		return false;
	}
}
// 步骤4.获取ticket
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$access_token";
$res = json_decode ( http_get ( $url ) );
$ticket = $res->ticket;
// 步骤5.生成wx.config需要的参数
$surl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$ws = getWxConfig( $ticket,$surl,time(),nonceStr(16) );
function getWxConfig($jsapiTicket,$url,$timestamp,$nonceStr) {
	$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
	$signature = sha1 ( $string );
	$WxConfig["appId"] = $appid;
	$WxConfig["nonceStr"] = $nonceStr;
	$WxConfig["timestamp"] = $timestamp;
	$WxConfig["url"] = $url;
	$WxConfig["signature"] = $signature;
	$WxConfig["rawString"] = $string;
	return $WxConfig;
}

?>

2、前端代码:引入分享js和配置分享

<!-- 分享 -->
<script type="text/javascript" src="js/jquery.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>

<script>
// 配置接口成功
wx.config({
	appId: '<?php echo $appid; ?>',
	timestamp: '<?php echo $ws["timestamp"]; ?>',
	nonceStr: '<?php echo $ws["nonceStr"]; ?>',
	signature: '<?php echo $ws["signature"]; ?>',
	jsApiList: [ // 必填,需要使用的JS接口列表
		'updateAppMessageShareData', // 自定义“分享给朋友”
		'updateTimelineShareData' // 自定义“分享到朋友圈”
	]
});
wx.ready(function() {
	wx.updateAppMessageShareData({
		title: '标题', // 分享标题
		desc: '描述', // 分享描述
		link: 'https://www.tpxhm.com/',
		imgUrl: 'https://www.tpxhm.com/static/uploads/thumb/20210730/61037fd654a66.jpg', // 分享图标
		success: function () {
			// 分享成功
		}
	})
	wx.updateTimelineShareData({
		title: '标题', // 分享标题
		link: 'https://www.tpxhm.com/'),
		imgUrl: 'https://www.tpxhm.com/static/uploads/thumb/20210730/61037fd654a66.jpg', // 分享图标
		success: function () {
			// 分享成功
		}
	})
});
</script>
【腾讯云】 爆款2核2G3M云服务器首年 61元,2核2G4M云服务器新老同享 99元/年,续费同价

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://tpxhm.com/adetail/808.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×