php开发调用微信js_sdk分享接口及其他调用

时间 11 : 37 , Nov 08,2016

#curl_get                          
function getcontents($url){
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $contents = curl_exec($ch);
    $status = curl_getinfo($ch);
    curl_close($ch);
    if($status['http_code'] == 200){
        return $contents;
    }
    return false;
}

首先是获取公众号的access_token,下面的access_token.php文件内容,直接get请求这个文件就可以获得access_token

//公众号appid
$appid="wxxxxxxxxxx";                           
//公众号AppSecret
$appsecret="xxxxxxxxxxxxxxxxxxxxxxxxx";         

//将获得保存在token.php的access_token
if(file_exists("token.php")){
require_once("token.php");
}

  //过期或者请求重新获取
if (empty($expire_time) || $expire_time < time() || $_GET['renew']==1) {       
    unlink("token.php");
    unlink("ticket.php");
      // 如果是企业号用以下URL获取access_token
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
    $res = json_decode(getcontents($url),true);
    $access_token = $res['access_token'];
      if ($access_token) {
        $expire_time = time() + 3600;
        $fp = fopen("token.php", "w");
        fwrite($fp, "");
        fclose($fp);
     }
  } else {
      $access_token = $access_token;
  }


if($_GET['all']==1){                    //回复全部字段json,expire_time为期满时间
 $data['expire_time'] = $expire_time;
 $data['access_token'] = $access_token;
echo json_encode($data);             
}else{                        
echo trim($access_token);
}

然后获取jsapi_ticket,下面的jsapi_ticket.php,直接get请求这个文件就可以获得jsapi_ticket

if(file_exists("ticket.php")){
require_once("ticket.php");
}

    if (empty($expire_time) || $expire_time < time() || $_GET['renew']==1) {
      //access_token.php文件路径
      $access_token=getcontents("access_token.php");  
      $access_token=trim($access_token);
      // 如果是企业号用以下 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=$access_token";                     //文件路径
      $res = json_decode(getcontents($url),true);

      $jsapi_ticket = $res['ticket'];
      if ($jsapi_ticket) {
        $expire_time = time() + 7000;
        $fp = fopen("ticket.php", "w");
        fwrite($fp, "");
        fclose($fp);
      }
    } else {
      $jsapi_ticket = $jsapi_ticket;
    }

if($_GET['all']==1){                     //回复全部字段json,expire_time为期满时间
$data['expire_time'] = $expire_time;
$data['jsapi_ticket'] = $jsapi_ticket;
echo json_encode($data);              
}else{              
echo trim($jsapi_ticket);
}

下面是share.php,我直接把这个文件require_once到页面里

$appid="xxxxxxxxxxx";
$jsapi_ticket = getcontents("jsapi_ticket.php");
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$timestamp = time();
$nonceStr = "heka".rand(1,1000)."string";
$string = "jsapi_ticket=".$jsapi_ticket."&noncestr=".$nonceStr.'&timestamp='.$timestamp."&url=$url";
$signature = sha1($string);
echo $shareScript='<script src=http://res.wx.qq.com/open/js/jweixin-1.0.0.js></script><script>
window.shareData = {
        "imgUrl": "分享图片",
        "sendFriendLink": "分享链接",
        "tTitle": "分享标题",
        "tContent":"分享描述"
    };
    wx.config({
        debug: false,
        appId: "'.$appid.'", 
        timestamp: '.$timestamp.',
        nonceStr:  "'.$nonceStr.'",
        signature: "'.$signature.'",
        jsApiList: ["onMenuShareTimeline",
            "onMenuShareAppMessage",
            "onMenuShareQQ",
            "onMenuShareWeibo",
            "chooseImage",
            "uploadImage",
            "startRecord",
            "stopRecord",
            "onVoiceRecordEnd",
            "playVoice",
            "pauseVoice",
            "stopVoice",
            "onVoicePlayEnd",
            "uploadVoice",
            "downloadVoice" 
            ]
    });
    
    wx.ready(function (res) {
         //分享到朋友圈
        wx.onMenuShareTimeline({
            title: window.shareData.tTitle, // 分享标题
            link: window.shareData.sendFriendLink, // 分享链接
            imgUrl: window.shareData.imgUrl, // 分享图标
            success: function (res) {
                // 用户确认分享后执行的回调函数
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
            }
        });
        //分享给朋友
        wx.onMenuShareAppMessage({
            title: window.shareData.tTitle, // 分享标题
            desc: window.shareData.tContent, // 分享描述
            link: window.shareData.sendFriendLink, // 分享链接
            imgUrl: window.shareData.imgUrl, // 分享图标
            success: function (res) {
                // 用户确认分享后执行的回调函数
            },
            cancel: function () {
                // 用户取消分享后执行的回调函
            }
        });
        //分享到qq
        wx.onMenuShareQQ({
            title: window.shareData.tTitle, // 分享标题
            desc: window.shareData.tContent, // 分享描述
            link: window.shareData.sendFriendLink, // 分享链接
            imgUrl: window.shareData.imgUrl, // 分享图标
            success: function (res) {
                // 用户确认分享后执行的回调函数
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
            }
        });
        //分享到微博
        wx.onMenuShareWeibo({
            title: window.shareData.tTitle, // 分享标题
            desc: window.shareData.tContent, // 分享描述
            link: window.shareData.sendFriendLink, // 分享链接
            imgUrl: window.shareData.imgUrl, // 分享图标
            success: function (res) {
                // 用户确认分享后执行的回调函数
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
            }
        });
    });
</script>';



留 言

  

网站简介

作者的个人博客,记事本,记录工作和学习中遇到的问题和一些基础功能开发!
好好学习,天天向上!

常用网站

Copyright © 2016-2024 LXC.All rights reserved. 皖ICP备2023025580号-1