必须要认证的公众号才有分享的权限
要用jssdk来做
网上其他的方法都没有效果,最多只能有个标题,描述跟缩略图是没有的
注意:
1、微信公众号要有分享的权限才能进行后面的步骤
2、公众号后台填写js安全域名,IP白名单也要填好
1、前端代码
<!--引入js文件-->
<script src="js/jquery.min.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
$(function() {
//微信分享
$.ajax({
url:"http://www.****.com/getJsAPITicket.php", //接口地址,填你自己的,这里主要获取配置文件的参数
type:"Get",
dataType:"json",
data:{"url": encodeURIComponent(window.location.href.split('#')[0])}, //注意,这里的url后端要获取的,不这么写会报错
success:function(data){
wxstart(data, "");
}
});
});
function wxstart(data,url){
wx.config({
debug: false,
appId: data.appId,
timestamp: data.timestamp,
nonceStr: data.nonceStr,
signature: data.signature,
jsApiList: [
'onMenuShareTimeline',
'onMenuShareAppMessage' //这里还可以有qq的接口之类的,看你需求
]
});
wx.ready(function () {
var shareData = {
title: '********', // 标题
desc: '************', // 描述
link: 'http://www.******.com/', // 分享的URL,必须和当前打开的网页的URL是一样的
imgUrl: 'http://www.******.com/logo.png' // 缩略图地址
};
wx.onMenuShareTimeline(shareData);
wx.onMenuShareAppMessage(shareData);
});
wx.error(function (res) {
alert(res.errMsg);//错误提示
});
}
2、php后端代码 getJsAPITicket.php
$jssdk = new JSSDK("wx3********e70", "6202c9b5a*******c6492271525");
$signPackage = $jssdk->GetSignPackage();
echo json_encode($signPackage);die;
class JSSDK {
private $appId;
private $appSecret;
public function __construct($appId, $appSecret) {
$this->appId = $appId;
$this->appSecret = $appSecret;
}
public function getSignPackage() {
$jsapiTicket = $this->getJsApiTicket();
//$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url = urldecode($_GET['url']);
$timestamp = time();
$nonceStr = $this->createNonceStr();
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
$signature = sha1($string);
$signPackage = array(
"appId" => $this->appId,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
);
return $signPackage;
}
private 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;
}
private function getJsApiTicket() {
// jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode(file_get_contents("jsapi_ticket.json"));
if ($data->expire_time < time()) {
$accessToken = $this->getAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
$res = json_decode($this->httpGet($url));
$ticket = $res->ticket;
if ($ticket) {
$data->expire_time = time() + 7000;
$data->jsapi_ticket = $ticket;
$fp = fopen("jsapi_ticket.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
} else {
$ticket = $data->jsapi_ticket;
}
return $ticket;
}
private function getAccessToken() {
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode(file_get_contents("access_token.json"));
if ($data->expire_time < time()) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
$res = json_decode($this->httpGet($url));
$access_token = $res->access_token;
if ($access_token) {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$fp = fopen("access_token.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}
private function httpGet($url) {
$headers = array('User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36');
$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_TIMEOUT, 20);
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_HTTPHEADER, $headers);
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;
}
}
}
3、排查错误方法
1、用微信开发者工具打开网页,出错他会提示
2、判断access_token 是否能够正确获取
上 微信公众平台接口调试工具调试 https://mp.weixin.qq.com/debug/
可能原因就是js安全域名、IP白名单、appSecret出错
3、判断signature是否能正确获取
http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign
4、其他可能的错误
https://mp.weixin.qq.com/wiki?action=doc&id=mp1421141115&t=0.20000340685689832#62 看这里面的附录5,一点点对照着来。