時(shí)間:2023-11-18 20:30:01 | 來源:網(wǎng)站運(yùn)營
時(shí)間:2023-11-18 20:30:01 來源:網(wǎng)站運(yùn)營
如何使用微信JS-SDK實(shí)際分享功能?:'updateAppMessageShareData', //分享到朋友圈'updateTimelineShareData',//分享給朋友
http://res.wx.qq.com/open/js/jweixin-1.6.0.js
3、前端初始化配置,即將后端獲取到的參數(shù)都用到前端上去,并且在jsApiList這個(gè)數(shù)組當(dāng)中添加你要使用的API。 // 初始化配置 wx.config({ debug: true, // 正式上線后成false不在彈出調(diào)試信息 appId: '<?php echo $appid;?>', timestamp: '<?php echo $timestamp;?>', nonceStr: '<?php echo $nonceStr;?>', signature: '<?php echo $signature;?>', jsApiList: [ // 所有要調(diào)用的 API 都要加到這個(gè)列表中 'updateAppMessageShareData', //分享到朋友圈 'updateTimelineShareData',//分享給朋友 ] });
4、然后調(diào)用ready函數(shù)來驗(yàn)證是否配置成功wx.ready(function () { //實(shí)例化API }})
信息驗(yàn)證失敗會(huì)執(zhí)行error函數(shù),如簽名過期導(dǎo)致驗(yàn)證失敗,具體錯(cuò)誤信息可以打開config的debug模式查看,也可以在返回的res參數(shù)中查看。wx.error(function(res){ // config alert(res);});
5、實(shí)例化API//分享到朋友圈wx.updateTimelineShareData({ title: '', // 分享標(biāo)題 link: '', // 分享鏈接,該鏈接域名或路徑必須與當(dāng)前頁面對(duì)應(yīng)的公眾號(hào)JS安全域名一致 imgUrl: '', // 分享圖標(biāo) success: function (res) { //分享成功 }})wx.updateAppMessageShareData({ title: '', // 分享標(biāo)題 desc: '', // 分享描述 link: '', // 分享鏈接,該鏈接域名或路徑必須與當(dāng)前頁面對(duì)應(yīng)的公眾號(hào)JS安全域名一致 imgUrl: '', // 分享圖標(biāo) success: function (res) { //分享成功 }})
<?php// 聲明頁面headerheader("Content-type:charset=utf-8");// 聲明APPID、APPSECRET$appid = "填寫你的APPID";$appsecret = "填寫你的APPSECRET";// 獲取access_token和jsapi_ticketfunction getToken(){ $file = file_get_contents("access_token.json",true);//讀取access_token.json里面的數(shù)據(jù) $result = json_decode($file,true);//判斷access_token是否在有效期內(nèi),如果在有效期則獲取緩存的access_token//如果過期了則請(qǐng)求接口生成新的access_token并且緩存access_token.jsonif (time() > $result['expires']){ $data = array(); $data['access_token'] = getNewToken(); $data['expires'] = time()+7000; $jsonStr = json_encode($data); $fp = fopen("access_token.json", "w"); fwrite($fp, $jsonStr); fclose($fp); return $data['access_token']; }else{ return $result['access_token']; }}//獲取新的access_tokenfunction getNewToken($appid,$appsecret){ global $appid; global $appsecret; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret.""; $access_token_Arr = file_get_contents($url); $token_jsonarr = json_decode($access_token_Arr, true); return $token_jsonarr["access_token"];}$access_token = getToken();//緩存jsapi_ticketfunction getTicket(){ $file = file_get_contents("jsapi_ticket.json",true);//讀取jsapi_ticket.json里面的數(shù)據(jù) $result = json_decode($file,true);//判斷jsapi_ticket是否在有效期內(nèi),如果在有效期則獲取緩存的jsapi_ticket//如果過期了則請(qǐng)求接口生成新的jsapi_ticket并且緩存jsapi_ticket.jsonif (time() > $result['expires']){ $data = array(); $data['jsapi_ticket'] = getNewTicket(); $data['expires'] = time()+7000; $jsonStr = json_encode($data); $fp = fopen("jsapi_ticket.json", "w"); fwrite($fp, $jsonStr); fclose($fp); return $data['jsapi_ticket']; }else{ return $result['jsapi_ticket']; }}//獲取新的access_tokenfunction getNewTicket($appid,$appsecret){ global $appid; global $appsecret; $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".getToken().""; $jsapi_ticket_Arr = file_get_contents($url); $ticket_jsonarr = json_decode($jsapi_ticket_Arr, true); return $ticket_jsonarr["ticket"];}$jsapiTicket = getTicket();// 動(dòng)態(tài)獲取URL$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";// 生成時(shí)間戳$timestamp = time();// 生成nonceStr$createNonceStr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";str_shuffle($createNonceStr);$nonceStr = substr(str_shuffle($createNonceStr),0,16);// 按照 key 值 ASCII 碼升序排序$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";// 按順序排列按sha1加密生成字符串$signature = sha1($string);?><!DOCTYPE html><html lang="zh_cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0"> <title>要分享的index模板頁面</title> </head> <body> <h3>已獲得updateAppMessageShareData</h3> <h3>已獲得updateTimelineShareData</h3> <h3>這兩個(gè)API權(quán)限</h3> <h3>請(qǐng)點(diǎn)擊右上角[···]體驗(yàn)</h3> </body></html> <!-- 引入微信分享js--> <script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> <script type="text/javascript"> // 初始化配置 wx.config({ debug: true, // 正式上線后改成false不在彈出調(diào)試信息 appId: '<?php echo $appid;?>', timestamp: '<?php echo $timestamp;?>', nonceStr: '<?php echo $nonceStr;?>', signature: '<?php echo $signature;?>', jsApiList: [ // 所有要調(diào)用的 API 都要加到這個(gè)列表中 'updateAppMessageShareData', //分享到朋友圈 'updateTimelineShareData',//分享給朋友 ] }); // 配置完成后會(huì)調(diào)用ready函數(shù) wx.ready(function (res) { //分享到朋友圈 wx.updateTimelineShareData({ title: '調(diào)用成功!分享到朋友圈!', // 分享標(biāo)題 link: 'http://www.liketube.cn/res/share.php', // 分享鏈接,該鏈接域名或路徑必須與當(dāng)前頁面對(duì)應(yīng)的公眾號(hào)JS安全域名一致 imgUrl: 'http://www.liketube.cn/res/1.jpg', // 分享圖標(biāo) success: function (res) { // 分享成功 } }) wx.updateAppMessageShareData({ title: '調(diào)用成功!分享給好友!', // 分享標(biāo)題 desc: '微信JSSDK1.6新版分享接口成功調(diào)用!', // 分享描述 link: 'http://www.liketube.cn/res/share.php', // 分享鏈接,該鏈接域名或路徑必須與當(dāng)前頁面對(duì)應(yīng)的公眾號(hào)JS安全域名一致 imgUrl: 'http://www.liketube.cn/res/1.jpg', // 分享圖標(biāo) success: function (res) { // 分享成功 } }) }); //錯(cuò)誤返回信息 wx.error(function(res){ // config信息驗(yàn)證失敗會(huì)執(zhí)行error函數(shù),如簽名過期導(dǎo)致驗(yàn)證失敗,具體錯(cuò)誤信息可以打開config的debug模式查看,也可以在返回的res參數(shù)中查看,對(duì)于SPA可以在這里更新簽名。 alert(res); }); </script>
關(guān)鍵詞:實(shí)際,功能,使用
客戶&案例
營銷資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。