微信分享demo

PHP實現微信分享朋友圈
1、先加載微信自帶的接口代碼

  1. <?php 
  2. class JSSDK { 
  3.   private $appId; 
  4.   private $appSecret; 
  5.  
  6.   public function __construct($appId, $appSecret) { 
  7.     $this->appId = $appId; 
  8.     $this->appSecret = $appSecret; 
  9.   } 
  10.  
  11.   public function getSignPackage() { 
  12.     $jsapiTicket = $this->getJsApiTicket(); 
  13.  
  14.     // 注意 URL 一定要動態獲取,不能 hardcode. 
  15.     $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; 
  16.     $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 
  17.  
  18.     $timestamp = time(); 
  19.     $nonceStr = $this->createNonceStr(); 
  20.  
  21.     // 這裏參數的順序要按照 key 值 ASCII 碼升序排序 
  22.     $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; 
  23.  
  24.     $signature = sha1($string); 
  25.  
  26.     $signPackage = array( 
  27.       "appId"     => $this->appId, 
  28.       "nonceStr"  => $nonceStr, 
  29.       "timestamp" => $timestamp, 
  30.       "url"       => $url, 
  31.       "signature" => $signature, 
  32.       "rawString" => $string 
  33.     ); 
  34.     return $signPackage;  
  35.   } 
  36.  
  37.   private function createNonceStr($length = 16) { 
  38.     $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 
  39.     $str = ""; 
  40.     for ($i = 0; $i < $length; $i++) { 
  41.       $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); 
  42.     } 
  43.     return $str; 
  44.   } 
  45.  
  46.   private function getJsApiTicket() { 
  47.     // jsapi_ticket 應該全局存儲與更新,以下代碼以寫入到文件中做示例 
  48.     $data = json_decode($this->get_php_file("jsapi_ticket.php")); 
  49.     if ($data->expire_time < time()) { 
  50.       $accessToken = $this->getAccessToken(); 
  51.       // 如果是企業號用以下 URL 獲取 ticket 
  52.       // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; 
  53.       $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; 
  54.       $res = json_decode($this->httpGet($url)); 
  55.       $ticket = $res->ticket; 
  56.       if ($ticket) { 
  57.         $data->expire_time = time() + 7000; 
  58.         $data->jsapi_ticket = $ticket; 
  59.         $this->set_php_file("jsapi_ticket.php", json_encode($data)); 
  60.       } 
  61.     } else { 
  62.       $ticket = $data->jsapi_ticket; 
  63.     } 
  64.  
  65.     return $ticket; 
  66.   } 
  67.  
  68.   private function getAccessToken() { 
  69.     // access_token 應該全局存儲與更新,以下代碼以寫入到文件中做示例 
  70.     $data = json_decode($this->get_php_file("access_token.php")); 
  71.     if ($data->expire_time < time()) { 
  72.       // 如果是企業號用以下URL獲取access_token 
  73.       // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret"; 
  74.       $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; 
  75.       $res = json_decode($this->httpGet($url)); 
  76.       $access_token = $res->access_token; 
  77.       if ($access_token) { 
  78.         $data->expire_time = time() + 7000; 
  79.         $data->access_token = $access_token; 
  80.         $this->set_php_file("access_token.php", json_encode($data)); 
  81.       } 
  82.     } else { 
  83.       $access_token = $data->access_token; 
  84.     } 
  85.     return $access_token; 
  86.   } 
  87.  
  88.   private function httpGet($url) { 
  89.     $curl = curl_init(); 
  90.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
  91.     curl_setopt($curl, CURLOPT_TIMEOUT, 500); 
  92.     // 爲保證第三方服務器與微信服務器之間數據傳輸的安全性,所有微信接口採用https方式調用,必須使用下面2行代碼打開ssl安全校驗。 
  93.     // 如果在部署過程中代碼在此處驗證失敗,請到 http://curl.haxx.se/ca/cacert.pem 下載新的證書判別文件。 
  94.     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); 
  95.     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); 
  96.     curl_setopt($curl, CURLOPT_URL, $url); 
  97.  
  98.     $res = curl_exec($curl); 
  99.     curl_close($curl); 
  100.  
  101.     return $res; 
  102.   } 
  103.  
  104.   private function get_php_file($filename) { 
  105.     return trim(substr(file_get_contents($filename), 15)); 
  106.   } 
  107.   private function set_php_file($filename, $content) { 
  108.     $fp = fopen($filename, "w"); 
  109.     fwrite($fp, "<?php exit();?>" . $content); 
  110.     fclose($fp); 
  111.   } 
  112. }

複製代碼

 

2、修改自己的配製信息用微信瀏覽器打開就可以了

  1. <?php 
  2. require_once "jssdk.php"; 
  3. $appid = 'wx110'; 
  4. $APPSECRET = '110'; 
  5. $jssdk = new JSSDK($appid, $APPSECRET); 
  6. $signPackage = $jssdk->GetSignPackage(); 
  7. ?> 
  8.  
  9. <!DOCTYPE html> 
  10. <html lang="en"> 
  11.     <head> 
  12.         <meta charset="UTF-8"> 
  13.         <title>js微信自定義分享標題、鏈接和圖標</title> 
  14.          <meta name="keywords" content="js微信分享,php微信分享" /> 
  15.         <meta name="description" content="PHP自定義微信分享內容,包括標題、圖標、鏈接等,分享成功和取消有js回調函數。" /> 
  16.     </head> 
  17.     <body> 
  18.  
  19.     </body> 
  20.     <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> 
  21.     <script> 
  22.         /* 
  23.          * 注意: 
  24.          * 1. 所有的JS接口只能在公衆號綁定的域名下調用,公衆號開發者需要先登錄微信公衆平臺進入“公衆號設置”的“功能設置”裏填寫“JS接口安全域名”。 
  25.          * 2. 如果發現在 Android 不能分享自定義內容,請到官網下載最新的包覆蓋安裝,Android 自定義分享接口需升級至 6.0.2.58 版本及以上。 
  26.          * 3. 常見問題及完整 JS-SDK 文檔地址:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html 
  27.          * 
  28.          * 開發中遇到問題詳見文檔“附錄5-常見錯誤及解決辦法”解決,如仍未能解決可通過以下渠道反饋: 
  29.          * 郵箱地址:[email protected] 
  30.          * 郵件主題:【微信JS-SDK反饋】具體問題 
  31.          * 郵件內容說明:用簡明的語言描述問題所在,並交代清楚遇到該問題的場景,可附上截屏圖片,微信團隊會盡快處理你的反饋。 
  32.          */ 
  33.         wx.config({ 
  34.             debug: false, 
  35.             appId: '<?php echo $appid; ?>', 
  36.             timestamp: <?php echo $signPackage["timestamp"]; ?>, 
  37.             nonceStr: '<?php echo $signPackage["nonceStr"]; ?>', 
  38.             signature: '<?php echo $signPackage["signature"]; ?>', 
  39.             jsApiList: [ 
  40.                'onMenuShareTimeline' 
  41.             ] 
  42.         }); 
  43.         wx.ready(function() { 
  44.             wx.onMenuShareTimeline({ 
  45.                 title: '二當家的', // 分享標題 
  46.                 link: 'http://www.erdangjiade.com/', // 分享鏈接 
  47.                 imgUrl: '', // 分享圖標 
  48.                 success: function() { 
  49.                     // 用戶確認分享後執行的回調函數 
  50.                 }, 
  51.                 cancel: function() { 
  52.                     // 用戶取消分享後執行的回調函數 
  53.                 } 
  54.             }); 
  55.         }); 
  56.     </script> 
  57.     <p style="text-align: center;color:red;font-size:20px;margin-top: 120px">請用微信瀏覽器打開,並打開右上方按鈕。分享到朋友圈試試。</p> 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章