2023-07-03 3217
接口分享使用方法:
1、PHP代码(wx/wxapi.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和配置分享
<!--引入-->
<?php
require("wx/wxapi.php");
?>
<!-- 分享 -->
<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: [
'onMenuShareTimeline',
'onMenuShareAppMessage',
'hideOptionMenu',
'showOptionMenu',
'chooseImage',
'previewImage',
'uploadImage',
'closeWindow',
],
});
wx.ready(function() {
var shareData = {
title: '小蚂蚁定制开发',
desc: '致力于企业网络定制开发服务',
link: 'http://wx.xmyfw.com.cn',
imgUrl: 'http://wx.xmyfw.com.cn/images/logos.jpg'
};
wx.onMenuShareAppMessage(shareData);
wx.onMenuShareTimeline(shareData);
});
</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: 'http://wx.xmyfw.com.cn',
imgUrl: 'http://wx.xmyfw.com.cn/images/logos.jpg', // 分享图标
success: function () {
// 分享成功
}
})
wx.updateTimelineShareData({
title: '标题', // 分享标题
link: 'http://wx.xmyfw.com.cn'),
imgUrl: 'http://wx.xmyfw.com.cn/images/logos.jpg', // 分享图标
success: function () {
// 分享成功
}
})
});
</script>
