html页面纯php原生编写微信分享修改标题和图标

来源:互联网 发布:js react触发ajax 编辑:程序博客网 时间:2024/06/01 08:49
<!DOCTYPE html>

<?php
    
    ////access token
     if(strpos($_SERVER["HTTP_USER_AGENT"], "MicroMessenger"))
     {
         
        if(!isset($GLOBALS['access_token']) || !isset($GLOBALS['effectivetm'])|| (time() - $GLOBALS['effectiveTokentm']) >3600 )
        {
            $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_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);
            $jsoninfo = json_decode($output, true);
            $access_token = $jsoninfo["access_token"];
            $effectiveTokentm = time();
            //setcookie("access_token", $access_token, time()+3600);
         }
echo $GLOBALS['access_token'];
echo "====<br />";
        ////ticket
        if(!isset($GLOBALS['ticket']) || !isset($GLOBALS['effectiveTickettm']) || (time() - $GLOBALS['effectiveTickettm']) > 3600)
        {
            $token =$GLOBALS['access_token'];
            $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$token";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);
            $jsoninfo = json_decode($output, true);
            $ticket = $jsoninfo["ticket"];
            $effectiveTickettm = time();
            //setcookie("ticket", $ticket, time()+3600);
        }
         
     }
echo $GLOBALS['ticket'];
echo "====<br />";
////nonceStr
     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;
    }
    
////sign package 签名url必须动态获取
    function getSignPackage() {
        if(strpos($_SERVER["HTTP_USER_AGENT"], "MicroMessenger"))
        {
            $jsapiTicket = $GLOBALS['ticket'];
            $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
            $timestamp = time();
            $nonceStr = createNonceStr();
            // 这里参数的顺序要按照 key 值 ASCII 码升序排序
            $ddstring = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
            
            echo $ddstring."=====<br />";

            $signature = sha1($ddstring);

            $signPackage = array("appId" => '***********', "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string);
            return $signPackage;
        }
        
        return null;
        
    }
    
    $packdata = getSignPackage();
    
    
    print_r($packdata);

?>

<html prefix="og: http://ogp.me/ns#" class="csstransforms csstransforms3d csstransitions js_active  vc_desktop  vc_transform  vc_transform  vc_transform " id="ls-global" lang="zh-CN"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
     
<title>weixin fenxiang DESCANDICON</title>

<!--  weixin begin -->
  <?php
    if(strpos($_SERVER["HTTP_USER_AGENT"], "MicroMessenger")){
         echo '<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"> </script>';
    }else{
        echo '<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"> </script>';
    }
  ?>
<script>


wx.config({
        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
        appId: '***********', // 必填,公众号的唯一标识
        timestamp: "<?php echo $packdata['timestamp'] ?>", // 必填,生成签名的时间戳
        nonceStr: '<?php echo $packdata['nonceStr'] ?>',//'15dbdf29c2d34416', // 必填,生成签名的随机串
        signature: '<?php echo $packdata['signature'] ?>',// 必填,签名,见附录1
        jsApiList: ['onMenuShareAppMessage','onMenuShareTimeline','onMenuShareQQ','onMenuShareWeibo','onMenuShareQZone'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
    });
    
      wx.ready(function () {
          alert("00000");
        // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
        console.log('ready');
        wx.onMenuShareAppMessage({
            title: '5周年', // 分享标题
            desc: '5周年', // 分享描述
            link: 'http://www.baidu.com?', // 分享链接
            imgUrl: 'http://www.baidu.com/1111.png', // 分享图标
            type: '', // 分享类型,music、video或link,不填默认为link
            dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
            success: function () {
                // 用户确认分享后执行的回调函数
                alert(1);
                console.log('成功1');
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
                alert(2);
                console.log('失败');
            }
        });
        wx.onMenuShareTimeline({
            title: '5周年', // 分享标题
            desc: '5周年', // 分享描述
            link: 'http://www.baidu.com?', // 分享链接
            imgUrl: 'http://www.baidu.com/1111.png', // 分享图标

            type: '', // 分享类型,music、video或link,不填默认为link
            dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
            success: function () {
                // 用户确认分享后执行的回调函数
                console.log('成功1');
                alert("2222");
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
                console.log('失败');
                alert("3333");
            }
        });
        wx.onMenuShareQQ({
            title: '5周年', // 分享标题
            desc: '5周年', // 分享描述
            link: 'http://www.baidu.com?', // 分享链接
            imgUrl: 'http://www.baidu.com/1111.png', // 分享图标

            type: '', // 分享类型,music、video或link,不填默认为link
            dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
            success: function () {
                // 用户确认分享后执行的回调函数
                console.log('成功1');
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
                console.log('失败');
            }
        });
        wx.onMenuShareWeibo({
            title: '5周年', // 分享标题
            desc: '5周年', // 分享描述
            link: 'http://www.baidu.com?', // 分享链接
            imgUrl: 'http://www.baidu.com/1111.png', // 分享图标

            type: '', // 分享类型,music、video或link,不填默认为link
            dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
            success: function () {
                // 用户确认分享后执行的回调函数
                console.log('成功1');
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
                console.log('失败');
            }
        });
        wx.onMenuShareQZone({
            title: '5周年', // 分享标题
            desc: '5周年', // 分享描述
            link: 'http://www.baidu.com?', // 分享链接
            imgUrl: 'http://www.baidu.com/1111.png', // 分享图标

            type: '', // 分享类型,music、video或link,不填默认为link
            dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
            success: function () {
                // 用户确认分享后执行的回调函数
                console.log('成功1');
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数
                console.log('失败');
            }
        });
    });
    wx.error(function (res) {
        alert("00000ERROR");
        // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
        console.log('error');
    });
    wx.checkJsApi({
        jsApiList: ['chooseImage'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
        success: function (res) {
            alert("11111");
            // 以键值对的形式返回,可用的api值true,不可用为false
            // 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
            console.log('check');
        }
    });

</script>

<!--  weixin end -->

    <body >
    
        <?php
    if(strpos($_SERVER["HTTP_USER_AGENT"], "MicroMessenger")){
        echo "您正在使用微信浏览器访问该页面";
    }else{
        echo "<span>HTTP/1.1 401 Unauthorized</span>";
    }

    ?>

<p>坑1:签名时url必须动态获取</p>

<p>坑2:jsApiList接口顺序必须按照微信公司规定来</p>

         

   <a href="#" id="toTop" style="display: none;"><span id="undefined"></span>undefined</a>


</body>


</html>

阅读全文
0 0
原创粉丝点击