EHUM
11/5/2018 - 11:09 AM

WXshare

PHP+jQuery Ajax微信分享

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>微信转发分享</title>
</head>
<body>
    <header><h1>DEMO</h1></header>
</body>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="text/javascript">
    //分享信息
    var thiswstitle = "这是一个微信分享的DEMO"; //分享标题
	var thiswsdesc = "可以使用封面图、标题、描述"; //分享描述
	var thiswsimg = "http://txaw2.cn/fm15.jpg"; //分享封面图
</script>
<script src="js/wxshare.js"></script>
</html>
<?php
	
    // 步骤1.设置appid和appsecret
    $appid = 'wx9a575d9652924f60';
    $appsecret = 'ab977889c4ce3ffe71ea5d7c2cbc6c86';
 
    // 步骤2.生成签名的随机串
    function nonceStr($length){
        $str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';//62个字符
        $strlen = 62;
        while($length > $strlen){
        $str .= $str;
        $strlen += 62;
        }
        $str = str_shuffle($str);
        return substr($str,0,$length);
    }
 
    // 步骤3.获取access_token
    $result = http_get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret);
    $json = json_decode($result,true);
    $access_token = $json['access_token'];
 
    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;
        }
    }
 
    // 步骤4.获取ticket
    $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$access_token";
    $res = json_decode ( http_get ( $url ) );
    $ticket = $res->ticket;
 
 
    // 步骤5.生成wx.config需要的参数
    $surl = ''.$_SERVER['HTTP_REFERER'];
    $ws = getWxConfig( $ticket,$surl,time(),nonceStr(16) );
 
    function getWxConfig($jsapiTicket,$url,$timestamp,$nonceStr) {
        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
        $signature = sha1 ( $string );
 
        $WxConfig["appId"] = $appid;
        $WxConfig["nonceStr"] = $nonceStr;
        $WxConfig["timestamp"] = $timestamp;
        $WxConfig["url"] = $url;
        $WxConfig["signature"] = $signature;
        $WxConfig["rawString"] = $string;
        return $WxConfig;
    }
    
    echo json_encode($ws);
?>
var _this = {};
var uuid = new XMLHttpRequest();
uuid.open("GET", "js/uuid.php", false);
uuid.onload = function() {
    _this = JSON.parse(this.response);
}
uuid.send();
//      console.log(_this);
//		console.log(_this.timestamp);
//		console.log(_this.nonceStr);
//		console.log(_this.signature);
wx.config({
    debug: false,
    appId: 'wx9a575d9652924f60',//<?php echo $ws["appId"]; ?>
    timestamp: _this.timestamp,//<?php echo $ws["timestamp"]; ?>
    nonceStr: _this.nonceStr,//<?php echo $ws["nonceStr"]; ?>
    signature: _this.signature,//<?php echo $ws["signature"]; ?>
    jsApiList: [
        'checkJsApi',
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo',
        'onMenuShareQZone',
    ]
});

var wstitle = thiswstitle;
var wsdesc = thiswsdesc;
var wslink = _this.url;
var wsimg = thiswsimg;
			

wx.ready(function () {
    // 分享到朋友圈
    wx.onMenuShareTimeline({
        title: wstitle,
        link: wslink,
        imgUrl: wsimg,
        success: function () {
           // alert('分享成功');
        },
        cancel: function () {
        }
    });
 
    // 分享给朋友
    wx.onMenuShareAppMessage({
        title: wstitle,
        desc: wsdesc,
        link: wslink,
        imgUrl: wsimg,
        success: function () {
          //alert('分享成功');
        },
        cancel: function () {
        }
    });
 
    // 分享到QQ
    wx.onMenuShareQQ({
        title: wstitle,
        desc: wsdesc,
        link: wslink,
        imgUrl: wsimg,
        success: function () {
           // alert('分享成功');
        },
        cancel: function () {
        }
    });
 
    // 微信到腾讯微博
    wx.onMenuShareWeibo({
        title: wstitle,
        desc: wsdesc,
        link: wslink,
        imgUrl: wsimg,
        success: function () {
           // alert('分享成功');
        },
        cancel: function () {
        }
    });
 
    // 分享到QQ空间
    wx.onMenuShareQZone({
        title: wstitle,
        desc: wsdesc,
        link: wslink,
        imgUrl: wsimg,
        success: function () {
          //  alert('分享成功');
        },
        cancel: function () {
        }
    });
 
});