php實現微信公衆號自定義分享內容的方法

微信公衆號號在手機中通過api接口可以實現自定義分享內容了,下面我們來看這個接口的實現步驟.

一、準備階段

公衆號一個,微網站一個.

二、綁定域名

先登錄微信公衆平臺進入“公衆號設置”的“功能設置”裏填寫“JS接口安全域名”.

備註:登錄後可在“開發者中心”查看對應的接口權限。

三、代碼

<?php

//curl獲取請求文本內容

function get_curl_contents($url, $method ='GET', $data = array()) {

  if ($method == 'POST') {

    //使用crul模擬

    $ch = curl_init();

    //禁用htt<a href="/fw/photo.html" target="_blank">ps</a>

    <a href="/tags.php/curl_setopt/" target="_blank">curl_setopt</a>($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    //允許請求以文件流的形式返回

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    curl_setopt($ch, CURLOPT_POST, 1);

    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);

    curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 30);

    curl_setopt($ch, CURLOPT_URL, $url);

    $result = curl_exec($ch); //執行發送

    curl_close($ch);

  }else {

    if (ini_get('allow_<a href="/tags.php/fopen/" target="_blank">fopen</a>_url') == '1') {

      $result = file_get_contents($url);

    }else {

      //使用crul模擬

      $ch = curl_init();

      //允許請求以文件流的形式返回

      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

      //禁用https

      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

      curl_setopt($ch, CURLOPT_URL, $url);

      $result = curl_exec($ch); //執行發送

      curl_close($ch);

    }

  }

  return $result;

}

//獲取微信公從號access_token

function wx_get_token() {

  $AppID = '1235464654';//AppID(應用ID)

  $AppSecret = '705641465sdfasdf456465a4sdf';//AppSecret(應用密鑰)

  $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$AppID.'&secret='.$AppSecret;

  $res = get_curl_contents($url);

  $res = json_decode($res, true);

  //這裏應該把access_token緩存起來,至於要怎麼緩存就看各位了,有效期是7200s

  return $res['access_token'];

}

//獲取微信公從號ticket

function wx_get_jsapi_ticket() {

  $url = sprintf("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi", wx_get_token());

  $res = get_curl_contents($url);

  $res = json_decode($res, true);

  //這裏應該把access_token緩存起來,至於要怎麼緩存就看各位了,有效期是7200s

  return $res['ticket'];

}

$wx = array();

//生成簽名的時間戳

$wx['timestamp'] = time();

//生成簽名的隨機串

$wx['noncestr'] = 'Wm3WZYTPz0wzccnW';

//jsapi_ticket是公衆號用於調用微信JS接口的臨時票據。正常情況下,jsapi_ticket的有效期爲7200秒,通過access_token來獲取。

$wx['jsapi_ticket'] = wx_get_jsapi_ticket();

//分享的地址,注意:這裏是指當前網頁的URL,不包含#及其後面部分,曾經的我就在這裏被坑了,所以小夥伴們要小心了

$wx['url'] = 'http://www.baidu.com';

$string = sprintf("jsapi_ticket=%s&noncestr=%s&timestamp=%s&url=%s", $wx['jsapi_ticket'], $wx['noncestr'], $wx['timestamp'], $wx['url']);

//生成簽名

$wx['signature'] = sha1($string);

/*

注意事項

簽名用的noncestr和timestamp必須與wx.config中的nonceStr和timestamp相同。

簽名用的url必須是調用JS接口頁面的完整URL。

出於安全考慮,開發者必須在服務器端實現簽名的邏輯。

*/

?>

四、視圖顯示

在需要調用JS接口的頁面引入如下JS文件,支持https:http://res.wx.qq.com/open/js/jweixin-1.0.0.js

通過config接口注入權限驗證配置.

<script>

//通過config接口注入權限驗證配置

wx.config({

  debug : false,

  appId : 'AppID',

  timestamp : '<?php echo $wx["timestamp"];?>',

  nonceStr : '<?php echo $wx["noncestr"];?>',

  signature : '<?php echo $wx["signature"];?>',

  jsApiList : ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo']

});

wx.ready(function(){

  var

    s_title = '分享標題',  // 分享標題

    s_link = '分享鏈接',  // 分享鏈接

    s_desc = '分享描述',  //分享描述

    s_imgUrl = '分享圖片'; // 分享圖標

  //朋友圈

  wx.onMenuShareTimeline({

    title: s_title, // 分享標題

    link: s_link, // 分享鏈接

    imgUrl: s_imgUrl, // 分享圖標

    success: function () { },

    cancel: function () { }

  });

  //發送給好友

  wx.onMenuShareAppMessage({

    title: s_title, // 分享標題

    desc: s_desc, // 分享描述

    link: s_link, // 分享鏈接

    imgUrl: s_imgUrl, // 分享圖標

    type: '', // 分享類型,music、video或link,不填默認爲link

    dataUrl: '', // 如果type是music或video,則要提供數據鏈接,默認爲空

    success: function () {},

    cancel: function () {}

  });

  //QQ好友

  wx.onMenuShareQQ({

    title: s_title, // 分享標題

    desc: s_desc, // 分享描述

    link: s_link, // 分享鏈接

    imgUrl: s_imgUrl, // 分享圖標

    success: function () { },

    cancel: function () { }

  });

  //騰訊微博

  wx.onMenuShareWeibo({

    title: s_title, // 分享標題

    desc: s_desc, // 分享描述

    link: s_link, // 分享鏈接

    imgUrl: s_imgUrl, // 分享圖標

    success: function () { },

    cancel: function () { }

  });

});

</script>

五、大功告成

基本上的流程就是這樣了,比較麻煩的一點就是生成簽名那一塊,注意一點就行了.

總結:以上就是本篇文的全部內容,希望能對大家的學習有所幫助。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章