前提:
1、绑定js安全域名
2、公众号必须是认证的,才有权限
附上已经测试好代码
页面引用这段代码
(function(){if(window.navigator.userAgent.match(/MicroMessenger/i)){document.write("
jweixin.php 文件如下:
$appId = 'wx';
$appSecret = '';
$timestamp = time();
$nonceStr = createNonceStr();
$jsapiTicket = getJsApiTicket();
$url = isset($_GET['url'])?urldecode($_GET['url']):$_SERVER['HTTP_REFERER'];
$signature = sha1("jsapi_ticket={$jsapiTicket}&noncestr={$nonceStr}×tamp={$timestamp}&url={$url}");
echo '(function(){';
echo 'var wx_share=wx_share||{imgUrl:';
echo isset($_GET['imgUrl'])?$_GET['imgUrl']:'document.getElementById("wx_share_img")?document.getElementById("wx_share_img").src:"http://jc.qcqhn.com/images/weixin.jpg"';
echo ',title:';
echo isset($_GET['title'])?$_GET['title']:'document.title';
echo ',desc:';
echo isset($_GET['desc'])?$_GET['desc']:'document.getElementsByTagName("meta")["description"].content';
echo ',link:"';
echo $url;
echo '",success:function (){}};';
echo 'wx.config({debug:false,appId:"';
echo $appId;
echo '",timestamp:"';
echo $timestamp;
echo '",nonceStr:"';
echo $nonceStr;
echo '",signature: "';
echo $signature;
echo '",jsApiList:["checkJsApi","onMenuShareTimeline","onMenuShareAppMessage","updateTimelineShareData","updateAppMessageShareData","onMenuShareWeibo"]});';
echo 'wx.ready(function (){wx.onMenuShareTimeline(wx_share);wx.onMenuShareAppMessage(wx_share);wx.updateTimelineShareData(wx_share);wx.onMenuShareWeibo(wx_share);wx.updateAppMessageShareData(wx_share);});';
echo '})();';
function createNonceStr($length = 16) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$chars_end = strlen($chars) - 1;
$str = '';
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, $chars_end), 1);
}
return $str;
}
function getJsApiTicket($cache='jsapi_ticket.php') {
global $appId, $appSecret;
$data = json_decode(file_get_contents($cache));
if ($data->expire_time < time()) {
$accessToken = getAccessToken();
// 如果是企业号用以下 URL 获取 ticket
// $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token={$accessToken}";
$res = json_decode(httpGet($url));
$ticket = $res->ticket;
if ($ticket) {
$data->expire_time = time() + 7000;
$data->jsapi_ticket = $ticket;
file_put_contents($cache, json_encode($data));
}else{
file_put_contents('jsapi_ticket_log.php', var_export($res,true));
}
} else {
$ticket = $data->jsapi_ticket;
}
return $ticket;
}
function getAccessToken($cache='access_token.php') {
global $appId, $appSecret;
$data = json_decode(file_get_contents($cache));
if ($data->expire_time < time()) {
// 如果是企业号用以下URL获取access_token
// $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
$res = json_decode(httpGet($url));
$access_token = $res->access_token;
if ($access_token) {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
file_put_contents($cache, json_encode($data));
}else{
file_put_contents('access_token_log.php', var_export($res,true));
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}
function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
//代码无误