這篇文章主要介紹PHP實現分享朋友圈demo源碼,感興趣的朋友參考下。
1、先載入內建的介面代碼
<?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 一定要動態擷取,不能 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(); // 這裡參數的順序要按照 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 應該全域儲存與更新,以下代碼以寫入到檔案中做樣本 $data = json_decode($this->get_php_file("jsapi_ticket.php")); if ($data->expire_time < time()) { $accessToken = $this->getAccessToken(); // 如果是企業號用以下 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; $this->set_php_file("jsapi_ticket.php", json_encode($data)); } } else { $ticket = $data->jsapi_ticket; } return $ticket; } private function getAccessToken() { // access_token 應該全域儲存與更新,以下代碼以寫入到檔案中做樣本 $data = json_decode($this->get_php_file("access_token.php")); if ($data->expire_time < time()) { // 如果是企業號用以下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; $this->set_php_file("access_token.php", json_encode($data)); } } 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); // 為保證第三方伺服器與伺服器之間資料轉送的安全性,所有介面採用https方式調用,必須使用下面2行代碼開啟ssl安全校正。 // 如果在部署過程中代碼在此處驗證失敗,請到 http://curl.haxx.se/ca/cacert.pem 下載新的認證判別檔案。 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } private function get_php_file($filename) { return trim(substr(file_get_contents($filename), 15)); } private function set_php_file($filename, $content) { $fp = fopen($filename, "w"); fwrite($fp, "<?php exit();?>" . $content); fclose($fp); } }
2、修改自己的配製資訊用瀏覽器開啟就可以了
<?php require_once "jssdk.php"; $appid = 'wx110'; $APPSECRET = '110'; $jssdk = new JSSDK($appid, $APPSECRET); $signPackage = $jssdk->GetSignPackage(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>js自訂分享標題、連結和表徵圖</title> <meta name="keywords" content="js分享,php分享" /> <meta name="description" content="PHP自訂分享內容,包括標題、表徵圖、連結等,分享成功和取消有js回呼函數。" /> </head> <body> </body> <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> <script> /* * 注意: * 1. 所有的JS介面只能在公眾號綁定的網域名稱下調用,公眾號開發人員需要先登入公眾平台進入“公眾號設定”的“功能設定”裡填寫“JS介面安全網域名稱”。 * 2. 如果發現在 Android 不能分享自訂內容,請到官網下載最新的包覆蓋安裝,Android 自訂分享介面需升級至 6.0.2.58 版本及以上。 * 3. 常見問題及完整 JS-SDK 文檔地址:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html * * 開發中遇到問題詳見文檔“附錄5-常見錯誤及解決辦法”解決,如仍未能解決可通過以下渠道反饋: * 郵箱地址:weixin-open@qq.com * 郵件主題:【JS-SDK反饋】具體問題 * 郵件內容說明:用簡明的語言描述問題所在,並交代清楚遇到該問題的情境,可附上截屏圖片,團隊會儘快處理你的反饋。 */ wx.config({ debug: false, appId: '<?php echo $appid; ?>', timestamp: <?php echo $signPackage["timestamp"]; ?>, nonceStr: '<?php echo $signPackage["nonceStr"]; ?>', signature: '<?php echo $signPackage["signature"]; ?>', jsApiList: [ 'onMenuShareTimeline' ] }); wx.ready(function() { wx.onMenuShareTimeline({ title: '二當家的', // 分享標題 link: 'http://www.erdangjiade.com/', // 分享連結 imgUrl: '', // 分享表徵圖 success: function() { // 使用者確認分享後執行的回呼函數 }, cancel: function() { // 使用者取消分享後執行的回呼函數 } }); }); </script> <p style="text-align: center;color:red;font-size:20px;margin-top: 120px">請用瀏覽器開啟,並開啟右上方按鈕。分享到朋友圈試試。</p> </html>
相關推薦:
有關朋友圈的文章推薦10篇
Smobiler仿朋友圈的訊息代碼執行個體
圖文詳解.Net語言Smobiler開發之如何仿朋友圈的訊息樣式
朋友圈怎麼只發文字 php寫的朋友圈