手机页面开发中的微信分享js与PHP实现

首先看js部分。

<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="text/javascript">
	var shareurl = 'http://www.baidu.com';
	var imgUrl = 'http://www.baidu.com/share.jpg';
	window.shareData = {
		"imgUrl" : imgUrl,
		"timeLineLink" : shareurl,
		"sendFriendLink" : shareurl,
		"weiboLink" : shareurl,
		"tTitle" : "分享到朋友圈的标题",
		"tContent" : '分享到朋友圈的内容',
		"fTitle" : "分享给朋友的标题",
		"fContent" : '分享给朋友的内容',
		"wContent" : '分享到微博的内容'
	};
	wx.config({
		debug: false,
		appId: 'wx23424234243423424',  //微信的appid需要在公众平台生成
		timestamp: '<? echo $timestamp; ?>', //这是由php部分生成的
		nonceStr: '<? echo $nonceStr; ?>', //这是由php部分生成的
		signature: '<? echo $signature; ?>', //这是由php部分生成的
		jsApiList: [
			'onMenuShareTimeline',
			'onMenuShareAppMessage',
			'onMenuShareQQ',
			'onMenuShareWeibo',
		]
	});
	wx.ready(function () {
		wx.onMenuShareAppMessage({
			title: shareData.tTitle,
			desc:  shareData.tContent,
			link:  shareurl,
			imgUrl: imgUrl,
			trigger: function (res) {},A
			success: function (res) {},
			cancel: function (res) {},
			fail: function (res) {}
		});
		wx.onMenuShareTimeline({
			title: shareData.tTitle,
			link: shareurl,
			imgUrl: imgUrl,
			trigger: function (res) {},
			success: function (res) {},
			cancel: function (res) {},
			fail: function (res) {}
		});
		wx.onMenuShareQQ({
			title: shareData.tTitle,
			desc:  shareData.tContent,
			link:  shareurl,
			imgUrl: imgUrl,
			trigger: function (res) {},
			success: function (res) {},
			cancel: function (res) {},
			fail: function (res) {}
		});
		wx.onMenuShareWeibo({
			title: shareData.tTitle,
			desc:  shareData.tContent,
			link:  shareurl,
			imgUrl: imgUrl,
			trigger: function (res) {},
			success: function (res) {},
			cancel: function (res) {},
			fail: function (res) {}
		});
	});
</script>

然后看PHP生成代码的部分。

<?
	//get token
	$token = fopen("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx23223532323232&secret=54947456456465456465465464","rb");
	$tokenRes = "";
		
	while (!feof($token)) {  $tokenRes .= fread($token, 1000); }

	fclose($token);
	$token_res = json_decode($tokenRes)->access_token;

	//get ticket
	$ticket = fopen("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=". $token_res ."&type=jsapi","rb");
	$ticketRes = "";
	while (!feof($ticket)) { $ticketRes .= fread($ticket, 1000); }

	fclose($ticket);
	$ticket_res = json_decode($ticketRes)->ticket;

	//create nonceStr
	$allChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $nonceStr = "";
    for ($i = 0; $i < 16; $i++) {
      $nonceStr .= substr($allChar, mt_rand(0, strlen($allChar) - 1), 1);
    }

	//create timestamp
	$timestamp = time();

	//get signature
	$tpStr = 'jsapi_ticket='.$ticket_res.'&noncestr='.$nonceStr.'&timestamp='.$timestamp.'&url=http://'.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
	$signature = sha1($tpStr);

	//echo "token:".$token_res;
	//echo "ticket:".$ticket_res;
	//echo "nonceStr:".$nonceStr;
	//echo "timestamp:".$timestamp;
	//echo "signature:".$signature;
?>

这样就可以在首页中include这段php来生成代码。

本文链接:https://www.92ez.com/?action=show&id=58
!!! 转载请先联系non3gov@gmail.com授权并在显著位置注明作者和原文链接 !!! 小黑屋
提示:技术文章有一定的时效性,请先确认是否适用你当前的系统环境。

关键词: 微信分享 , arduino

上一篇: My GSM Sniffer Kit come soon.
下一篇: 近期计划调整

相关文章
访客评论
目前还没有人评论,您发表点看法?
发表评论

评论内容 (必填):