微信分享JS接口

没想到微信接口的突破口实在JS分享这里,今天搞定了这个分享。

1)PHP端需要获取signature的值,因接口调用次数有限制,所以加入了mem存入取出的值,7100秒后过期:

$shaMem = 'shaTicket';
//!$strMem = MemHelper::Get($shaMem)
if(!$strMem = MemHelper::Get($shaMem)){
    //获取相关数据star
    $str = curl_https("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=微信平台获取&secret=微信平台获取");
    $str = json_decode($str,true);
    $access_token = $str['access_token'];
    $str = curl_https("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi");
    $str = json_decode($str,true);
    $ticket = $str['ticket'];
    $str = "jsapi_ticket=".$ticket."&noncestr=Wm3WZYTPz0wzccnW&timestamp=1414587457&url=具体网址";
    $strMem = sha1($str);
    //获取相关数据end
    MemHelper::Set($shaMem, $strMem, 7100);
}

function curl_https($url, $data=array(), $header=array(), $timeout=30){  
  
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查  
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  // 从证书中检查SSL加密算法是否存在  
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    $response = curl_exec($ch);
    if($error=curl_error($ch)){
        die($error);  
    }  
    curl_close($ch);  
    return $response;  
}

2)页面底部加入JS代码,前期测试建议打开debug:

wx.config({
    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
    appId: '微信后台获取', // 必填,公众号的唯一标识
    timestamp: 1414587457, // 必填,生成签名的时间戳
    nonceStr: 'Wm3WZYTPz0wzccnW', // 必填,生成签名的随机串
    signature: '<?php echo $strMem;?>',// 必填,签名,见附录1
    jsApiList: [
        'checkJsApi',
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo',
        'hideMenuItems',
        'showMenuItems',
        'hideAllNonBaseMenuItem',
        'showAllNonBaseMenuItem',
        'translateVoice',
        'startRecord',
        'stopRecord',
        'onRecordEnd',
        'playVoice',
        'pauseVoice',
        'stopVoice',
        'uploadVoice',
        'downloadVoice',
        'chooseImage',
        'previewImage',
        'uploadImage',
        'downloadImage',
        'getNetworkType',
        'openLocation',
        'getLocation',
        'hideOptionMenu',
        'showOptionMenu',
        'closeWindow',
        'scanQRCode',
        'chooseWXPay',
        'openProductSpecificView',
        'addCard',
        'chooseCard',
        'openCard'
        ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function(){
    wx.onMenuShareAppMessage({
        title: '测试标题', // 分享标题
        desc: '测试描述……', // 分享描述
        link: '具体网址', // 分享链接
        imgUrl: '图片网址', // 分享图标
        type: '', // 分享类型,music、video或link,不填默认为link
        dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
        success: function () { 
            // 用户确认分享后执行的回调函数
        },
        cancel: function () { 
            // 用户取消分享后执行的回调函数
        }
    });    
    wx.onMenuShareTimeline({
        title: '测试标题', // 分享标题
        link: "具体网址",
        imgUrl: "图片网址", // 分享图标
        success: function () { 
            // 用户确认分享后执行的回调函数
        },
        cancel: function () { 
            // 用户取消分享后执行的回调函数
        }
    });
});

 

posted @ 2015-07-28 11:55  ddliang  阅读(1492)  评论(0编辑  收藏  举报