我们现在用微信微博很多了,经常看见有人在群和朋友圈中分享文章,,我们分享出去的时候只是空白链接,但是别人分享的时候却带着漂亮的图片和标题描述,他们是怎么做到的呢??微信分享的代码分析:
下面我分享两段代码给大家看看就明白了:
1.当然是微信的SDK了,拿到微信的SDK参数:
class JSSDK {
private $appId;
private $appSecret;
private $cache_key_jsticket;
private $cache_key_token;
public function __construct($appId='', $appSecret='') {
$this->appId = $appId;
$this->appSecret = $appSecret;
$this->cache_key_jsticket = "wx_jsticket_" . $this->appId;
$this->cache_key_token = "wx_token_" . $this->appId;
}
public function GetSignPackage() {
$jsapiTicket = $this->getJsApiTicket();
if (!empty($_SERVER['HTTP_X_PROTO']) && strtoupper($_SERVER['HTTP_X_PROTO']) == 'SSL') {
$protocol = 'https';
} else {
$protocol = isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'https';
}
$protocol .= '://';
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$timestamp = time();
$nonceStr = $this->createNonceStr();
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
$signature = sha1($string);
$signPackage = array(
"jsapiTicket"=>$jsapiTicket,
"appId" => $this->appId,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
);
return $signPackage;
}
/**
*生成16位随机字符串
*@param int
*@return string
*/
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 应该全局存储与更新,以下代码以写入到文件中做示例
$mc = new RedisManager();
$data = json_decode($mc->get( $this->cache_key_jsticket ), true);
if ( $data['expire_time'] < time() || empty($data['jsapi_ticket']) ) {
$accessToken = $this->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?access_token=$accessToken&type=jsapi";
$res = json_decode($this->httpGet($url), true);
if( $res['errcode'] == 40001 ){
$mc->set($this->cache_key_jsticket, 0, '');
$accessToken = $this->getAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$accessToken&type=jsapi";
$res = json_decode($this->httpGet($url), true);
}
$ticket = $res['ticket'];
if ($ticket) {
$data['expire_time'] = time() + 7000;
$data['jsapi_ticket'] = $ticket;
$mc->set($this->cache_key_jsticket, 7000, json_encode($data));
}
} else {
$ticket = $data['jsapi_ticket'];
}
return $ticket;
}
private function getAccessToken() {
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例
$mc= new Redis();
$data = json_decode($mc->get( $this->cache_key_token ), true);
if ( $data['expire_time'] < time() || empty($data['access_token']) ) {
$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), true);
$access_token = $res['access_token'];
if ($access_token) {
$data['expire_time'] = time() + 7000;
$data['access_token'] = $access_token;
$mc->set( $this->cache_key_token , 7000, json_encode($data));
}
} else {
$access_token = $data['access_token'];
}
return $access_token;
}
/**
*获取网页access_token
*@param string
*@return string
*/
public function wap_access_token($code){
$appid = $this->appId;
$secret = $this->appSecret;
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$rs = json_decode($this->httpGet($url),true);
return $rs;
}
}
2.接下来是最关键的代码,涉及到js参数的运用:
<script src="//res.wx.qq.com/open/js/jweixin-1.1.0.js"></script>
<script>
var signPackage = eval(([$signPackage]));
var wxShareData1 = {
"title": "这一刻xxx",
"desc": "描述描述描述",
"link": location.href,
"imgUrl": "http://www.baidu.com/logo.jpg",
};
var userAgent = navigator.userAgent;
var pattern_wx = /MicroMessenger/;
var share = share || {};
var share = {
wxInit: function(){
wx.onMenuShareTimeline(wxShareData);
wx.onMenuShareAppMessage(wxShareData);
}
}
if(pattern_wx.test(userAgent)){
wx.config({
debug: false,
appId: signPackage['appId'],
timestamp: signPackage['timestamp'],
nonceStr: signPackage['nonceStr'],
signature: signPackage['signature'],
jsApiList: [
// 所有要调用的 API 都要加到这个列表中
'onMenuShareTimeline',
'onMenuShareAppMessage'
]
});
wx.ready(function () {
wxShareData = wxShareData1;
share.wxInit();
});
}
function setshare(){
if(pattern_wx.test(userAgent)){
share.wxInit();
}
if(pattern_weibo.test(userAgent)){
share.wbInit();
}
}
</script>
以上两个步骤就是调用微信的来分享文章的步骤了,快去试试把