最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php版微信公眾號(hào)自定義分享內(nèi)容實(shí)現(xiàn)方法

 更新時(shí)間:2016年09月22日 16:26:48   作者:smiling  
這篇文章主要介紹了php版微信公眾號(hào)自定義分享內(nèi)容實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了php實(shí)現(xiàn)微信公眾號(hào)自定義分享內(nèi)容的接口調(diào)用與相關(guān)使用技巧,需要的朋友可以參考下

本文實(shí)例講述了php版微信公眾號(hào)自定義分享內(nèi)容實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:

微信公眾號(hào)號(hào)在手機(jī)中通過api接口可以實(shí)現(xiàn)自定義分享內(nèi)容了,下面我們來看這個(gè)接口的實(shí)現(xiàn)步驟.

一、準(zhǔn)備階段

公眾號(hào)一個(gè),微網(wǎng)站一個(gè).

二、綁定域名

先登錄微信公眾平臺(tái)進(jìn)入“公眾號(hào)設(shè)置”的“功能設(shè)置”里填寫“JS接口安全域名”.

備注:登錄后可在“開發(fā)者中心”查看對(duì)應(yīng)的接口權(quán)限。

三、代碼

<?php
//curl獲取請(qǐng)求文本內(nèi)容
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);
    //允許請(qǐng)求以文件流的形式返回
    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); //執(zhí)行發(fā)送
    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();
      //允許請(qǐng)求以文件流的形式返回
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
      //禁用https
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($ch, CURLOPT_URL, $url);
      $result = curl_exec($ch); //執(zhí)行發(fā)送
      curl_close($ch);
    }
  }
  return $result;
}
//獲取微信公從號(hào)access_token
function wx_get_token() {
  $AppID = '1235464654';//AppID(應(yīng)用ID)
  $AppSecret = '705641465sdfasdf456465a4sdf';//AppSecret(應(yīng)用密鑰)
  $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);
  //這里應(yīng)該把a(bǔ)ccess_token緩存起來,至于要怎么緩存就看各位了,有效期是7200s
  return $res['access_token'];
}
//獲取微信公從號(hào)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);
  //這里應(yīng)該把a(bǔ)ccess_token緩存起來,至于要怎么緩存就看各位了,有效期是7200s
  return $res['ticket'];
}
$wx = array();
//生成簽名的時(shí)間戳
$wx['timestamp'] = time();
//生成簽名的隨機(jī)串
$wx['noncestr'] = 'Wm3WZYTPz0wzccnW';
//jsapi_ticket是公眾號(hào)用于調(diào)用微信JS接口的臨時(shí)票據(jù)。正常情況下,jsapi_ticket的有效期為7200秒,通過access_token來獲取。
$wx['jsapi_ticket'] = wx_get_jsapi_ticket();
//分享的地址,注意:這里是指當(dāng)前網(wǎng)頁的URL,不包含#及其后面部分,曾經(jīng)的我就在這里被坑了,所以小伙伴們要小心了
$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);
/*
注意事項(xiàng)
簽名用的noncestr和timestamp必須與wx.config中的nonceStr和timestamp相同。
簽名用的url必須是調(diào)用JS接口頁面的完整URL。
出于安全考慮,開發(fā)者必須在服務(wù)器端實(shí)現(xiàn)簽名的邏輯。
*/
?>

四、視圖顯示

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

通過config接口注入權(quán)限驗(yàn)證配置.

<script>
//通過config接口注入權(quán)限驗(yàn)證配置
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 = '分享標(biāo)題',  // 分享標(biāo)題
    s_link = '分享鏈接',  // 分享鏈接
    s_desc = '分享描述',  //分享描述
    s_imgUrl = '分享圖片'; // 分享圖標(biāo)
  //朋友圈
  wx.onMenuShareTimeline({
    title: s_title, // 分享標(biāo)題
    link: s_link, // 分享鏈接
    imgUrl: s_imgUrl, // 分享圖標(biāo)
    success: function () { },
    cancel: function () { }
  });
  //發(fā)送給好友
  wx.onMenuShareAppMessage({
    title: s_title, // 分享標(biāo)題
    desc: s_desc, // 分享描述
    link: s_link, // 分享鏈接
    imgUrl: s_imgUrl, // 分享圖標(biāo)
    type: '', // 分享類型,music、video或link,不填默認(rèn)為link
    dataUrl: '', // 如果type是music或video,則要提供數(shù)據(jù)鏈接,默認(rèn)為空
    success: function () {},
    cancel: function () {}
  });
  //QQ好友
  wx.onMenuShareQQ({
    title: s_title, // 分享標(biāo)題
    desc: s_desc, // 分享描述
    link: s_link, // 分享鏈接
    imgUrl: s_imgUrl, // 分享圖標(biāo)
    success: function () { },
    cancel: function () { }
  });
  //騰訊微博
  wx.onMenuShareWeibo({
    title: s_title, // 分享標(biāo)題
    desc: s_desc, // 分享描述
    link: s_link, // 分享鏈接
    imgUrl: s_imgUrl, // 分享圖標(biāo)
    success: function () { },
    cancel: function () { }
  });
});
</script>

五、大功告成

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

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP微信開發(fā)技巧匯總》、《PHP編碼與轉(zhuǎn)碼操作技巧匯總》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

沙坪坝区| 定远县| 闽清县| 平罗县| 沅江市| 兴隆县| 临城县| 沅江市| 阜城县| 平舆县| 左权县| 普格县| 高密市| 仁寿县| 和平区| 广水市| 宽城| 蕲春县| 普定县| 久治县| 黄山市| 家居| 双辽市| 南和县| 台东县| 百色市| 尉氏县| 乌兰察布市| 安新县| 旬邑县| 台山市| 永吉县| 石屏县| 浦县| 永丰县| 林西县| 武山县| 吴堡县| 赫章县| 青海省| 四平市|