前提是必須有公眾號(hào),且公眾號(hào)中需要填寫(xiě)你的域名為安全域名才能成功。以下分享接口已采用微信開(kāi)發(fā)文檔中最新的接口,網(wǎng)上很多人使用的原接口即將失效了。
第一步:部署jssdk.php到根目錄
第二步:微信公眾號(hào)中設(shè)置JS安全域
公眾號(hào)設(shè)置--功能設(shè)置--JS接口安全域名
第三步:增加模版變量,填入代碼,appid和appsecret填入你的微信公眾號(hào)相關(guān)的信息(模版變量緊挨著</body>,放在它前面):
<? //開(kāi)始微信分享代碼 require '../../jssdk.php'; $appid = '******'; $appsecret = '******'; $jssdk = new JSSDK($appid,$appsecret); $signPackage = $jssdk->GetSignPackage(); //結(jié)束微信分享代碼 ?> <script src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script> <script> // 注意:所有的JS接口只能在公眾號(hào)綁定的域名下調(diào)用,公眾號(hào)開(kāi)發(fā)者需要先登錄微信公眾平臺(tái)進(jìn)入“公眾號(hào)設(shè)置”的“功能設(shè)置”里填寫(xiě)“JS接口安全域名”。 // 如果發(fā)現(xiàn)在 Android 不能分享自定義內(nèi)容,請(qǐng)到官網(wǎng)下載最新的包覆蓋安裝,Android 自定義分享接口需升級(jí)至 6.0.2.58 版本及以上。 // 完整 JS-SDK 文檔地址:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html wx.config({ debug: !1, appId: '<?php echo $signPackage["appId"];?>', timestamp: <?php echo $signPackage["timestamp"];?>, nonceStr: '<?php echo $signPackage["nonceStr"];?>', signature: '<?php echo $signPackage["signature"];?>', jsApiList: [ "updateAppMessageShareData", "updateTimelineShareData", "onMenuShareWeibo", "onMenuShareQZone" ] }); wx.ready(function () { // 在這里調(diào)用 API wx.checkJsApi({ jsApiList: ["updateAppMessageShareData", "updateTimelineShareData", "onMenuShareWeibo", "onMenuShareQZone"], // 需要檢測(cè)的JS接口列表,所有JS接口列表見(jiàn)附錄2, success: function(res) { //alert(JSON.stringify(res)); if(res.errMsg !='checkJsApi:ok'){ alert('請(qǐng)升級(jí)您的微信版本'); return; } } }); //分享Demo //獲取“分享給朋友”按鈕點(diǎn)擊狀態(tài)及自定義分享內(nèi)容接口 wx.updateAppMessageShareData({ title: '<?=$navinfor[title]?>', // 分享標(biāo)題 desc: '<?=$navinfor[smalltext]?>', // 分享描述 link: 'http://www.xxx.com<?=$navinfor[titleurl]?>', // 分享鏈接 imgUrl: '<?=$navinfor[titlepic]?>', // 分享圖標(biāo) success: function () { // 設(shè)置成功 } }); //獲取“分享到朋友圈”按鈕點(diǎn)擊狀態(tài)及自定義分享內(nèi)容接口 wx.updateTimelineShareData({ title: '<?=$navinfor[title]?>', // 分享標(biāo)題 link: 'http://www.xxx.com<?=$navinfor[titleurl]?>', // 分享鏈接 imgUrl: '<?=$navinfor[titlepic]?>', // 分享圖標(biāo) success: function () { // 設(shè)置成功 } }); wx.onMenuShareWeibo({ title: '<?=$navinfor[title]?>', // 分享標(biāo)題 desc: '<?=$navinfor[smalltext]?>', // 分享描述 link: 'http://www.xxx.com<?=$navinfor[titleurl]?>', // 分享鏈接 imgUrl: '<?=$navinfor[titlepic]?>', // 分享圖標(biāo) success: function () { // 用戶(hù)確認(rèn)分享后執(zhí)行的回調(diào)函數(shù) }, cancel: function () { // 用戶(hù)取消分享后執(zhí)行的回調(diào)函數(shù) } }); }); </script>
<?php class JSSDK { private $appId; private $appSecret; public function __construct($appId, $appSecret) { $this->appId = $appId; $this->appSecret = $appSecret; } public function getSignPackage() { $jsapiTicket = $this->getJsApiTicket(); $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $timestamp = time(); $nonceStr = $this->createNonceStr(); // 這里參數(shù)的順序要按照 key 值 ASCII 碼升序排序 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; $signature = sha1($string); $signPackage = array( "appId" => $this->appId, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string ); return $signPackage; } private function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } private function getJsApiTicket() { // jsapi_ticket 應(yīng)該全局存儲(chǔ)與更新,以下代碼以寫(xiě)入到文件中做示例 $data = json_decode(file_get_contents("jsapi_ticket.json")); if ($data->expire_time < time()) { $accessToken = $this->getAccessToken(); $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; $res = json_decode($this->httpGet($url)); $ticket = $res->ticket; if ($ticket) { $data->expire_time = time() + 7000; $data->jsapi_ticket = $ticket; $fp = fopen("jsapi_ticket.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $ticket = $data->jsapi_ticket; } return $ticket; } private function getAccessToken() { // access_token 應(yīng)該全局存儲(chǔ)與更新,以下代碼以寫(xiě)入到文件中做示例 $data = json_decode(file_get_contents("access_token.json")); if ($data->expire_time < time()) { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}"; $res = json_decode($this->httpGet($url)); $access_token = $res->access_token; if ($access_token) { $data->expire_time = time() + 7000; $data->access_token = $access_token; $fp = fopen("access_token.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $access_token = $data->access_token; } return $access_token; } private function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } } ?>
版權(quán)聲明: 本站資源均來(lái)自互聯(lián)網(wǎng)或會(huì)員發(fā)布,如果侵犯了您的權(quán)益請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)刪除!謝謝!
轉(zhuǎn)載請(qǐng)注明: 帝國(guó)CMS微信分享帶縮略圖教程