<?php
namespace app\index\lib\wechat;
header("Access-Control-Allow-Origin:*");
class share
{
public $appid;
public $secret;
// 步骤1.appid和secret
//header("Access-Control-Allow-Origin:*");
//$appid = "appid";
//$secret = "secret";
public function __construct($appid,$secret)
{
$this->appid = $appid;
$this->secret = $secret;
}
// 步骤2.生成签名的随机串
public 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
public 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
public function getTicket(){
$result = $this->http_get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret='.$this->secret);
$json = json_decode($result,true);
$access_token = $json['access_token'];
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$access_token";
$res = json_decode ( $this->http_get ( $url ) );
return $res->ticket;
}
// 步骤5.生成wx.config需要的参数
//$surl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
//$ws = getWxConfig( $ticket,$surl,time(),nonceStr(16) );
// public function getWxConfig($jsapiTicket,$url,$timestamp,$nonceStr) {
public function getWxConfig() {
$jsapiTicket=$this->getTicket();
$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$timestamp = time();
$nonceStr = $this->nonceStr(rand(8,15));
$string = "jsapi_ticket=".$jsapiTicket."&noncestr=".$nonceStr."×tamp=".$timestamp."&url=".$url;
$signature = sha1 ($string);
$WxConfig["appId"] = $this->appid;
$WxConfig["nonceStr"] = $nonceStr;
$WxConfig["timestamp"] = $timestamp;
$WxConfig["url"] = $url;
$WxConfig["signature"] = $signature;
$WxConfig["rawString"] = $string;
return $WxConfig;
}
}
public function getWxShareConfig(){
$instanceWxShare = new \app\index\lib\wechat\share('appid','secret');
return $instanceWxShare->getWxConfig();
}
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<script>
// console.log(timestamp);
wx.config({
debug: false,
appId: '{$wxsc.appId}',
timestamp: '{$wxsc.timestamp}',
nonceStr: '{$wxsc.nonceStr}',
signature: '{$wxsc.signature}',
jsApiList: ['updateAppMessageShareData','updateTimelineShareData']
});
wx.ready(function () { //需在用户可能点击分享按钮前就先调用
wx.updateAppMessageShareData({
title: '医博:', // 分享标题
desc: '专业肛肠、胃肠、中医交流平台,为业界名专家们搭建教学、学术平台,为专业医生提供手术直播、科普交流基地,为学者提供学习、沟通、上升平台。', // 分享描述
link: 'https://wx.kmyebo.com', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: 'https://wx.kmyebo.com/yb_share_icon.jpg', // 分享图标
success: function () {
// 设置成功
}
})
});
wx.ready(function () { //需在用户可能点击分享按钮前就先调用
wx.updateTimelineShareData({
title: '医博:', // 分享标题
link: 'https://wx.kmyebo.com', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: 'https://wx.kmyebo.com', // 分享图标
success: function () {
// 设置成功
}
})
});
</script>
http://www.xmyfw.com.cn/pc/show.php?id=55
评论