//前端
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js" type="text/javascript" charset="utf-8" async defer></script>
<script>
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script>
var _timestamp = "{$timestamp}",
_nonceStr="{$noncestr}",
_signature= "{$signature}",
_shareUrl="{$url}";
{literal}
wx.config({
debug: false,
appId: "wxf6df8999ed90eb8b",
timestamp: _timestamp,
nonceStr: _nonceStr,
signature: _signature,
jsApiList: ['updateAppMessageShareData',
'updateTimelineShareData']
});
wx.ready(function () { //需在用户可能点击分享按钮前就先调用
wx.updateAppMessageShareData({
title: '测试分享', // 分享标题
desc: '测试分享', // 分享描述
link: _shareUrl,
imgUrl: 'http://vr.com/static/images/logo.png', // 分享图标
success: function () {
// 设置成功
alert("成功");
}
})
wx.updateTimelineShareData({
title: '测试分享', // 分享标题
desc: '测试分享', // 分享描述
link: _shareUrl,
imgUrl: 'http://vr..com/static/images/logo.png', // 分享图标
success: function () {
// 设置成功
}
})
});
{/literal}
</script>
//后台
<?php
//curl获取请求文本内容
//微信分享
define('IN_T', true);
require 'source/include/init.php';
function getJson($url)
{
$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);
return json_decode($output, true);
}
//获取微信公众号access_token
function wx_get_token()
{
$AppID = 'xxxx'; //AppID(应用ID)
$AppSecret = 'xxxxxxxx'; //AppSecret(应用密钥)
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $AppID . '&secret=' . $AppSecret;
$res = getJson($url);
return $res['access_token'];
}
//获取微信公众号ticket
function wx_get_jsapi_ticket()
{
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=". wx_get_token()."&type=jsapi";
$res = getJson($url);
return $res['ticket'];
}
//获取随机码
function getRandCode($randLength = 16, $includenumber = 1)
{
if ($includenumber) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQEST123456789';
} else {
$chars = 'abcdefghijklmnopqrstuvwxyz';
}
$len = strlen($chars);
$randStr = '';
for ($i = 0; $i < $randLength; $i++) {
$randStr .= $chars[mt_rand(0, $len - 1)];
}
return $randStr;
}
$wx = array();
$wx['timestamp'] = time();
$wx['noncestr'] = getRandCode();
$wx['jsapi_ticket'] = wx_get_jsapi_ticket();
$wx['url'] = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$wx['signature'] = "jsapi_ticket=".$wx['jsapi_ticket']."&noncestr=".$wx['noncestr']."×tamp=".$wx['timestamp']."&url=".$wx['url'];
$tp->assign("timestamp", $wx['timestamp']);
$tp->assign("noncestr", $wx['noncestr']);
$tp->assign("jsapi_ticket", $wx['jsapi_ticket']);
$tp->assign("url", $wx['url']);
$tp->assign("signature", sha1($wx['signature']));
$tp->display($_lang['moban'] . "/wxshare.tpl");
微信分享
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 项目中很多使用到微信分享的地方,对于前端来说也算很简单了,建议写该功能时使用微信调试工具,便于调试,直接上代码第一...
- // 微信官方demo => http://203.195.235.76/jssdk/var wx = requi...
- 1. 在微信开发者网站,注册应用id 2. 在资源下载页下载SDK文件 3. 集成SDK到Xcode 导入SDK到...