通过ajax调用微信分享的方法

新建一个fenxiang.php的文件,php代码如下:

<?php
function http_get($url) {
    $oCurl = curl_init();
    if (stripos($url, "https://") !== FALSE) {
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
    }
    curl_setopt($oCurl, CURLOPT_URL, $url);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
    $sContent = curl_exec($oCurl);
    $aStatus = curl_getinfo($oCurl);
    curl_close($oCurl);
    if (intval($aStatus["http_code"]) == 200) {
        return $sContent;
    } else {
        return false;
    }
}
function wx_get_token() {
        $AppID = 'wx343e2b6aec096e7d';
        $AppSecret = 'b0310c980b9e731a7968333b76f8c33b';
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $AppID . '&secret=' . $AppSecret;
        $res = http_get($url);
        $res = json_decode($res, true);
        return $res['access_token'];
    }
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 = http_get($url);
    $res = json_decode($res, true);
    return $res['ticket'];
}

function getSignature() {
    $signature = array();
    $signature['appid'] = 'wx343e2b6aec096e7d';
    $signature['timestamp'] = time();
    $signature['noncestr'] = generateNonceStr();
    $signature['jsapi_ticket'] = wx_get_jsapi_ticket();
    $signature['url'] = 'http://weixin.0411ganguo.com/cese/2019-06-15/1.html';
    $string = sprintf("jsapi_ticket=%s&noncestr=%s&timestamp=%s&url=%s", $signature['jsapi_ticket'], $signature['noncestr'], $signature['timestamp'], $signature['url']);
    $signature['signature'] = sha1($string);
    return $signature;
}

function generateNonceStr($length = 16) {$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";$str = "";for ($i = 0; $i < $length; $i++) { $str .= $chars[mt_rand(0, strlen($chars) - 1)];} return $str;}
$signature = getSignature();echo json_encode($signature);exit;
?>

jquery的ajax代码如下:

    <script src="http://res.wx.qq.com/open//js/jweixin-1.2.0.js"></script>
    <script>
        var json = {};
        $.ajax({
            url: '/lmcjl/fenxiang.php',
            timeout: 10000,
            type: 'get',
            async: false,
            cache: false,
            dataType: 'json',
            success: function(data) {
                json = data;
            },
            error: function(data) {}
        });
        wx.config({
            debug: false,
            appId: json.appid,
            timestamp: json.timestamp,
            nonceStr: json.noncestr,
            signature: json.signature,
            jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'getLocation']
        });
        wx.ready(function() {
            wx.checkJsApi({
                jsApiList: ['chooseImage'],
                success: function(res) {}
            });
            var shareUrl = 'http://weixin.0411ganguo.com';
            var shareUserPhone1 = "";
            var obj = { //朋友圈
                title: shareUserPhone1 + '给您的悄悄话',
                desc: '点击查看那些未曾对你说出口的话吧!',
                link: shareUrl,
                imgUrl: 'http://mxp.chinacmcp.com/html/images/shareBack.png',
                success: function(res) {
                    alert('老哥,分享成功中,数据统计中');
                }
            };
            var obj1 = { //好友
                title: shareUserPhone1 + '有些话,早就想对您说',
                desc: '点击查看那些未曾对你说出口的话吧!',
                link: shareUrl,
                imgUrl: 'http://mxp.chinacmcp.com/html/images/shareBack.png',
                success: function(res) {
                    alert('老哥,分享成功中,数据统计中');
                }
            };
            wx.onMenuShareAppMessage(obj1);
            wx.onMenuShareTimeline(obj);
        });
    </script>