您好,登錄后才能下訂單哦!
小編給大家分享一下PHP如何制作微信自定義分享,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
1、公眾號(hào)開(kāi)發(fā)信息配置
在開(kāi)發(fā)分享接口之前需要先配置公眾平臺(tái)配置信息,如下圖所示:點(diǎn)擊“開(kāi)發(fā)-基本信息”,可以看到公眾號(hào)開(kāi)發(fā)信息,在開(kāi)發(fā)者秘鑰哪里點(diǎn)擊“開(kāi)啟”,配置自己的秘鑰信息,配置成功后,保存開(kāi)發(fā)者ID(AppID)和開(kāi)發(fā)者密碼(AppSecret)
2、配置JS接口安全域名
點(diǎn)擊“設(shè)置與開(kāi)發(fā)--公眾號(hào)設(shè)置”,點(diǎn)擊JS接口安全域名后面的設(shè)置,按照要求添加js安全域名,如圖所示
3、PHP分享接口類
這里不多說(shuō),直接上代碼
class wechatClass { public $AppID; public $AppSecret; public $redirect_uri; public $DbSy; public $dump_url; public $scope; public function __construct() { parent::__construct(); $this->AppID = "開(kāi)發(fā)者密碼(AppSecret)"; $this->AppSecret = "開(kāi)發(fā)者密碼(AppSecret)"; } public function getSignPackage() { $jsapiTicket = $this->getJsApiTicket(); // 注意 URL 一定要?jiǎng)討B(tài)獲取,不能 hardcode. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = "{$protocol}{$_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); // var_dump($signature);die; $signPackage = array( "appId" => $this->AppID, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string ); return $signPackage; } public 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; } public function getJsApiTicket() { // jsapi_ticket 應(yīng)該全局存儲(chǔ)與更新,以下代碼以寫入到文件中做示例 //echo $_SERVER['DOCUMENT_ROOT']; $data = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT']."/static/yejuzhi/js/access_token.json")); // var_dump($data->expire_time);die; if ($data->expire_time < time()) { $accessToken = $this->getAccessToken(); // 如果是企業(yè)號(hào)用以下 URL 獲取 ticket // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; $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($_SERVER['DOCUMENT_ROOT']."/static/yejuzhi/js/access_token.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $ticket = $data->jsapi_ticket; } return $ticket; } public function getAccessToken() { // access_token 應(yīng)該全局存儲(chǔ)與更新,以下代碼以寫入到文件中做示例 $data = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT']."/static/yejuzhi/js/access_token.json")); if ($data->expire_time < time()) { // 如果是企業(yè)號(hào)用以下URL獲取access_token // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret"; $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($_SERVER['DOCUMENT_ROOT']."/static/yejuzhi/js/access_token.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $access_token = $data->access_token; } return $access_token; } public function httpGet($url){ $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } }
4、PHP文件配置
這里我是以TP5.1框架寫的,大家可以自行設(shè)置
$wx = new WeChatClass(); $sign = $wx->getSignPackage(); $this->assign('sign',$sign);
5、前端HTML代碼
前端HTML代碼請(qǐng)參考開(kāi)發(fā)文檔:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#111
這里我以自定義“分享給朋友”及“分享到QQ”按鈕的分享內(nèi)容(1.4.0)接口為例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>微信分享接口制作</title> </head> <body> <script src="http://res2.wx.qq.com/open/js/jweixin-1.6.0.js"></script> <script> wx.config({ debug: false, // 開(kāi)啟調(diào)試模式,調(diào)用的所有api的返回值會(huì)在客戶端alert出來(lái),若要查看傳入的參數(shù),可以在pc端打開(kāi),參數(shù)信息會(huì)通過(guò)log打出,僅在pc端時(shí)才會(huì)打印。 appId: '{$sign.appId}', // 必填,公眾號(hào)的唯一標(biāo)識(shí) timestamp: '{$sign.timestamp}', // 必填,生成簽名的時(shí)間戳 nonceStr: '{$sign.nonceStr}', // 必填,生成簽名的隨機(jī)串 signature: '{$sign.signature}',// 必填,簽名 jsApiList: [ "updateAppMessageShareData",] // 必填,需要使用的JS接口列表 }); wx.ready(function () { wx.updateAppMessageShareData({ title: '您的標(biāo)題', // 分享標(biāo)題 desc: '您的描述', // 分享描述 link: '您的分享鏈接', // 分享鏈接,該鏈接域名或路徑必須與當(dāng)前頁(yè)面對(duì)應(yīng)的公眾號(hào)JS安全域名一致 imgUrl: '您的分享圖標(biāo)', // 分享圖標(biāo) success: function () { /*alert('成功');*/ } }); }); </script> </body> </html>
看完了這篇文章,相信你對(duì)“PHP如何制作微信自定義分享”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。