朋友圈,qq,微博分享

原创 韩秀利 随笔 javascript 343阅读 2018-01-20 16:40:37 举报

<script type="text/javascript">
var timestamp ='<?echo $timestamp ?>';
var wxnonceStr ='<?echo $wxnonceStr ?>';
var wxSha1 ='<?echo $wxSha1 ?>';
wx.config({
debug: false,
appId: "微信订阅号appid",
timestamp: timestamp,
nonceStr: wxnonceStr,
signature: wxSha1,
jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo']
});
var title = '标题';
var imgurl = '图片;
var link = '链接';
var desc = '描述';

wx.ready(function(){
wx.checkJsApi({
jsApiList: [
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo'
]
});
/朋友圈/
wx.onMenuShareTimeline({
title: title,
desc: desc,
link: link,
imgUrl: imgurl
});
/朋友/
wx.onMenuShareAppMessage({
title: title,
desc: desc,
link: link,
imgUrl: imgurl
});
/QQ/
wx.onMenuShareQQ({
title: title,
link: link,
desc: desc,
imgUrl: imgurl
});
/微博/
wx.onMenuShareWeibo({
title: title,
link: link,
desc: desc,
imgUrl: imgurl,
});
});
</script>

<?php
date_default_timezone_set('Asia/Shanghai');

function wx_get_token() {
$current = time();
$arr=file_get_contents( "token.txt");
$arrtoken=json_decode($arr,true);
$token = $arrtoken['token'];
if (($current-$arrtoken['time']>3600)||(empty($token))) {
$res = file_get_contents('');

$res = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.'服务号APPID'.'&secret='.'服务号secret');
$res = json_decode($res, true);
$token = $res['access_token'];
$arr=array(
    "time"=> time(),
    "token"=>$token,
);
$fp=fopen("token.txt","wb");
fwrite($fp,json_encode($arr));
fclose($fp);
}
return $token;

}
function wx_get_jsapi_ticket(){
$current = time();
$ticket = "";
do{
$arrtic=file_get_contents( "ticket.txt");
$arrticket=json_decode($arrtic,true);
$ticket = $arrticket['ticket'];

    if (($current-$arrticket['time']<3600)) {
        break;
    }
    $arrto=file_get_contents( "token.txt");
    $arrtoken=json_decode($arrto,true);
    $token = $arrtoken['token'];

    if (($current-$arrtoken['time']>3600)||(empty($token))){
        wx_get_token();
    }
    $arrto=file_get_contents( "token.txt");
    $arrtoken=json_decode($arrto,true);
    $token = $arrtoken['token'];

    if (($current-$arrticket['time']>3600)||(empty($ticket))) {
    $url2 = sprintf("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi",
        $token);
    $res = file_get_contents($url2);
    $res = json_decode($res, true);
    $ticket = $res['ticket'];

    $arrt=array(
    "time"=> time(),
    "ticket"=>$ticket,
    );
    $fp=fopen("ticket.txt","wb");
    fwrite($fp,json_encode($arrt));
    fclose($fp);
    }
}while(0);

return $ticket;

}

$timestamp = time();
$wxnonceStr = "ceshi1234567";
$wxticket = wx_get_jsapi_ticket();
$wxOri = sprintf("jsapi_ticket=%s&noncestr=%s&timestamp=%s&url=%s",
$wxticket, $wxnonceStr, $timestamp,
'分享链接'
);
$wxSha1 = sha1($wxOri);
?>

评论 ( 0 )
最新评论
暂无评论

赶紧努力消灭 0 回复