当前位置 : 主页 > 网络编程 > PHP >

微信jssdk分享

来源:互联网 收集:自由互联 发布时间:2021-06-28
js部分 var weixin = {config:function(){//获取当前的网页地址var url=window.location.href.split("#")[0];//请求后端获得必要的签名$.get("jssdk.php",{"url":url},function(s){wx.congig({debug:false,//开启调试模式appId:s
js部分
var  weixin = {
	config:function(){

		//获取当前的网页地址
		var url=window.location.href.split("#")[0];
		//请求后端获得必要的签名
		$.get("jssdk.php",{"url":url},function(s){

			wx.congig({
				debug:false,//开启调试模式
				appId:s.appId,//必填,公众号的唯一标识
				timestamp:s.timestamp, //必填,生成签名的时间戳
				noceStr:s.noceStr, //必填,生成签名的的随机字符串
				signatrue:s.signatrue, //必填,签名,见附录一
				jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'chooseImage', 'uploadImage', 'downloadImage'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2

			})
		});
	},

	share:function (title,desc,url,img){
		//注意分享链接与图片地址的域名必须与公众号的安全域名一致
		var data = {
			title:title, //分享标题
			link: url, // 分享链接
			imgUrl: img, //分享图标
			desc: desc, //分享描述
			success: function (){
				//分享成功
			},
			error: function () {
				//分享失败
			}
		};

		//分享到朋友圈
		wx.onMenuShareTimeline({
			title:data.title,//分享标题
			link:data.url,//分享链接
			imgUrl:data.img,//分享图标
			success:data.success,//确认按钮
			cansl:data.ansel//取消按钮
		});
		//分享给好友
		wx.onMenuShareAppMessage({
			title:data.title,//分享标题
			link:data.link,//分享链接
			imgUrl:data.img,//分享图标
			type:"",//分享类型,music,video或link,不填默认为link
			dataUrl:"",//如果type是music或video,则要提供数据链接,默认为空
			success:data.success,//确认按钮
			cansl:data.ansel//取消按钮
		})
	}

}

$(window).load({
    wx.config();
    wx.ready(function(){
        wx.share("微信分享","微信分享","www.xxx.com","www.xxx.com/img.jpg");
    })
})
获取签名
  APPID,
		"timestamp"=>$timestamp,
		"nonceStr"=>$nonceStr,
		"signature"=>$signature
	);
	return json_encode($data);
}

//获取access_token  要设置IP白名单才能获取token
function getAccess(){
	$data = json_decode(file_get_contents("access.json"));
	if($data->time < time()){
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".SECRET;
		$res = json_decode(getHttp($url));
		$access = $res->access_token;
		if($access){
			$data->access = $access;
			$data->time = time()+7000;
//			chmod("access.json",0755);
			$fp = fopen("access.json","w");
			fwrite($fp, json_encode($data));
			fclose($fp);
		}
	}else{
			$access = $data->access;
	}
	return $access;
}

//获取jsApi_ticket票据
function getTicket(){
	$data = json_decode(file_get_contents("ticket.json"));
	if($data->time < time()){
		$accessToken = getAccess();
		$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
		$res = json_decode(getHttp($url));
		$ticket = $res->ticket;
		if($ticket){
			$data->ticket = $ticket;
			$data->time = time()+7000;
			$fp = fopen("ticket.json","w");
			fwrite($fp, json_encode($data));
			fclose($fp);
		}
	}else{
		$ticket = $data->ticket;
	}
	return $ticket;
}

//随机字符串
function createNonceStr($length = 16) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $str = "";
    for ($i = 0; $i < $length; $i++) {
      $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
  }

//curl封装
function getHttp($url) {
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_TIMEOUT, 500);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl, CURLOPT_URL, $url);
	$res = curl_exec($curl);
	curl_close($curl);
	return $res;
}

?>
网友评论