单页微信分享 php+html+js 混编最简版

版权声明:本文为Areom原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhanjianjiu/article/details/78373004

前提是:你有 appId appSecret

<?php function my_curl_get($url){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_POST,0); $res = curl_exec($ch); curl_close($ch); return $res; } //获取微信access_token function getaccess_token() { //appid与appsecret改成你自己的 $appid = '*****'; $appsecret = '*******'; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); $data = curl_exec($ch); curl_close($ch); $data = json_decode($data, true); return $data['access_token']; } //获取jsapi_ticket function getjsapi_ticket() { $access_token = getaccess_token(); $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token={$access_token}"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); $data = curl_exec($ch); curl_close($ch); $data = json_decode($data, true); return $data['ticket']; } //默认生成16位随机数 function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } //获取要排序的signature相关代码 function getSignPackage() { $jsapiTicket = getjsapi_ticket(); $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $timestamp = time(); $nonceStr = createNonceStr(); // 这里参数的顺序要按照 key 值 ASCII 码升序排序 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url"; $signature = sha1($string); $signPackage = array( "appId" => '*****', "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature ); return $signPackage; } $signPackage = getSignPackage(); $access_token = getaccess_token(); $news = array( "Title" => "****", "Description" => "******", "PicUrl" => "http://".$_SERVER['SERVER_NAME']."/img/feng.png", "Url" => "http://".$_SERVER['SERVER_NAME']."/index.php", "type" => 'link', // "dataUrl" =>"https://res.wx.qq.com/voice/getvoice?mediaid=MzA3OTk3NjI4OF81MDM1MTY0MDM=" ); ?>

<html>
<head>
    <link href="" rel="shortcut icon" type="image/vnd.microsoft.icon">
    <script src="./res/jquery.min.js"></script>
    <title></title>
    <script src="./res/jweixin-1.0.0.js"></script>
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" charset="utf-8">
</head>
<script type="text/javascript">
    wx.config({
        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
        appId: '<?php echo $signPackage["appId"]?>',
        timestamp: '<?php echo $signPackage["timestamp"]?>',
        nonceStr: '<?php echo $signPackage["nonceStr"]?>',
        signature: '<?php echo $signPackage["signature"]?>',
        jsApiList: [
            'checkJsApi',
            "onMenuShareTimeline",
            "onMenuShareAppMessage",
            "onMenuShareQQ",
            "onMenuShareWeibo",
            "startRecord",
            "stopRecord",
            "onVoiceRecordEnd",
            "playVoice",
            "pauseVoice",
            "stopVoice",
            "onVoicePlayEnd",
            "uploadVoice",
            "downloadVoice",
            "chooseImage",
            "previewImage",
            "uploadImage",
            "downloadImage",
            "translateVoice",
            "getNetworkType",
            "openLocation",
            "getLocation",
            "hideOptionMenu",
            "showOptionMenu",
            "hideMenuItems",
            "showMenuItems",
            "hideAllNonBaseMenuItem",
            "showAllNonBaseMenuItem",
            "closeWindow",
            "scanQRCode",
            "chooseWXPay",
            "openProductSpecificView",
            "addCard",
            "chooseCard",
            "openCard"
        ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
    });
    wx.ready(function () {
        wx.hideMenuItems({
            menuList: [
                'menuItem:copyUrl',
                'menuItem:originPage',
                'menuItem:openWithQQBrowser',] // 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3
        });
        //分享朋友圈
        wx.onMenuShareTimeline({
            title: '<?php echo $news["Title"]?>',
            link: '<?php echo $news["Url"]?>',
            imgUrl: '<?php echo $news["PicUrl"]?>',
            desc: '<?php echo $news["Description"]?>', // 分享描述
            type: '<?php echo $news["type"]?>', // 分享类型,music、video或link,不填默认为link
            success: function () {
                // 用户确认分享后执行的回调函数
                //$.post("./index.php?api=biaoshi&uid="+userid,function(res){
                //alert(res)
                //                    mui.toast(res.msg);
                //                    window.location.reload();
                //});//,'json'
            }
        });
        //分享给朋友
        wx.onMenuShareAppMessage({
            title: '<?php echo $news["Title"]?>',
            link: '<?php echo $news["Url"]?>',
            imgUrl: '<?php echo $news["PicUrl"]?>',
            desc: '<?php echo $news["Description"]?>', // 分享描述
            success: function () {

            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
            }
        });

    });
</script>
<body>
</body>
</html>

用法:自己改改 appId(有两处) appSecret

猜你喜欢

转载自blog.csdn.net/zhanjianjiu/article/details/78373004