最近在做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×tamp=$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>
关于简忆
简忆诞生的故事



粤ICP备16092285号
文章评论(0)