微信分享缩略图
$(document).ready(function(e) { var urll = location.href.split('#')[0]; $.ajax({ async:false, url:"php/fenxiang.php", data:{urll:urll}, type:"POST", dataType:"json", success: function(ress){ //console.log(ress);//查看返回结果 //执行JS_SDK wx.config({ debug: false, appId: ress.appid, timestamp: ress.timestamp, nonceStr: ress.nonceStr, signature: ress.signature, jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage'] }); wx.ready(function () { wx.onMenuShareTimeline({ link: urll, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致 imgUrl:"http://m.highshang.net/imgs/highshang.jpg", // 分享图标 success: function () { layer.msg('分享成功'); }, cancel: function () { layer.msg('您取消了分享'); } }); wx.onMenuShareAppMessage({ link: urll, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致 imgUrl: "http://m.highshang.net/imgs/highshang.jpg", // 分享图标 type: 'link', // 分享类型,music、video或link,不填默认为link success: function () { layer.msg('分享成功'); }, cancel: function () { layer.msg('您取消了分享'); } }); }); } }) }); //alert(location.href.split('#')[0]); //弹出的url必须与访问地址一致
<?php date_default_timezone_set("PRC"); //微信 $url = $_POST['urll'];//获取当前页面的url,接收请求参数 $root['url'] = $url; //获取access_token,并缓存 $token_file = fopen("token.txt", "r"); $rs = fgets($token_file); fclose($token_file); $attr = explode(',',$rs); $time2 = time(); $token = $attr[0]; if(intval($time2)-intval($attr[1])>7000) { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxb59cf3f3811db282&secret=bef9f19a79b93030b9552d0407354e19"; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); $output = json_decode($output, true); $token_file = fopen("token.txt","w");//打开token.txt文件 fwrite($token_file,$output['access_token'].','.time());//重写tken.txt全部内容 fclose($token_file);//关闭文件流 curl_close($curl); $token = $output['access_token']; } //获取jsapi_ticket,并缓存 $jsapi_ticket = fopen("jsapi_ticket.txt", "r"); $rs2 = fgets($jsapi_ticket); fclose($jsapi_ticket); $attr2 = explode(',',$rs2); $time3 = time(); $jssdk = $attr2[0]; if(intval($time3)-intval($attr2[1])>7000) { $ur = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$token&type=jsapi"; $res = file_get_contents($ur); $res = json_decode($res, true); $jssdk = $res['ticket']; $token_file2 = fopen("jsapi_ticket.txt","w");//打开token.txt文件 fwrite($token_file2,$jssdk.','.time());//重写tken.txt全部内容 fclose($token_file2);//关闭文件流 } $timestamp = time();//生成签名的时间戳 $metas = range(0, 9); $metas = array_merge($metas, range('A', 'Z')); $metas = array_merge($metas, range('a', 'z')); $nonceStr = ''; for ($i=0; $i < 16; $i++) { $nonceStr .= $metas[rand(0, count($metas)-1)];//生成签名的随机串 } $string1="jsapi_ticket=".$jssdk."&noncestr=$nonceStr"."×tamp=$timestamp"."&url=$url"; $signature=sha1($string1); $root['appid'] = 'wxb59cf3f3811db282'; $root['nonceStr'] = $nonceStr; $root['timestamp'] = $timestamp; $root['signature'] = $signature; echo json_encode($root); ?>