ajax 微信code获取_通过ajax调用微信分享的方法

本文介绍了一个使用PHP和jQuery AJAX实现的微信分享功能示例。该示例通过获取微信access_token和jsapi_ticket来生成用于微信JS-SDK配置的签名,进而实现页面分享至朋友圈和发送给朋友等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

新建一个fenxiang.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代码如下:

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);

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值