微信公众号分享
引入用于获取微信接口信息的php文件
<?php
// 步骤1.设置appid和appsecret
$appid = ''; //此处填写绑定的微信公众号的appid
$appsecret = ''; //此处填写绑定的微信公众号的密钥id
// 步骤2.生成签名的随机串
function nonceStr($length){
$str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJK1NGJBQRSTUVWXYZ';//随即串,62个字符
$strlen = 62;
while($length > $strlen){
$str .= $str;
$strlen += 62;
}
$str = str_shuffle($str);
return substr($str,0,$length);
}
// 步骤3.获取access_token
$result = http_get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret);
$json = json_decode($result,true);
$access_token = $json['access_token'];
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;
}
}
// 步骤4.获取ticket
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$access_token";
$res = json_decode( http_get ( $url ) );
$ticket = $res->ticket;
// 步骤5.生成wx.config需要的参数
$surl = $_GET['link'];
$ws = getWxConfig( $ticket,$surl,time(),nonceStr(16) );
function getWxConfig($jsapiTicket,$myurl,$timestamp,$nonceStr) {
global $appid;
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$myurl";
$signature = sha1 ( $string );
$WxConfig["appid"] = $appid;
$WxConfig["noncestr"] = $nonceStr;
$WxConfig["timestamp"] = $timestamp;
$WxConfig["url"] = $myurl;
$WxConfig["signature"] = $signature;
$WxConfig["rawstring"] = $string;
return $WxConfig;
}
echo json_encode($ws);
?>js调用
<script src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script type="text/javascript">
var stitle = "分享的标题";
var sdesc = "分享的描述";
var slink = "分享的链接";
var simgUrl = "分享的logo图标";
var url = encodeURIComponent(location.href.split("#")[0]);
$.ajax({
type: "get",
url: "http://51.zhuyouhui.net/share.php?link=" + url,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(e) {
var d = e.appid,
i = e.timestamp,
t = e.noncestr,
n = e.signature;
wx.config({
debug: 1,
appId: d,
timestamp: i,
nonceStr: t,
signature: n,
jsApiList: ["updateTimelineShareData", "updateAppMessageShareData"]
//jsApiList: ["onMenuShareTimeline", "onMenuShareAppMessage"]
}),
wx.ready(function() {
//分享到朋友圈
wx.updateTimelineShareData({
title: stitle,
desc: sdesc,
link: slink,
imgUrl: simgUrl
}),
//分享给朋友
wx.updateAppMessageShareData({
title: stitle,
desc: sdesc,
link: slink,
imgUrl: simgUrl
})
})
}
});
</script>开发放微信js:
1、按照说明文档完成步骤1.1.1



注意如果设置了以上还不行的话,要添加一下IP白名单



![[嘻嘻]](http://www.51zuso.com/static/tool/layui2.4.5/images/face/1.gif)