首先需要将以下函数写入TinkPHP的公用function.php文件中以便调用
// 基于ThinkPHP
/**
* php curl 请求链接
* 当$post_data为空时使用GET方式发送
* @param unknown $url
* @param string $post_data
* @return mixed
*/
function curlSend($url,$post_data=""){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
if($post_data != ""){
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
} /**
* 调用接口获取 $ACCESS_TOKEN
* 微信缓存 7200 秒,这里使用thinkphp的缓存方法
* @param unknown $APP_ID
* @param unknown $APP_SECRET
* @return Ambigous <mixed, Thinkmixed, object>
*/
function get_accesstoken($APP_ID,$APP_SECRET){
$ACCESS_TOKEN = S($APP_ID);
if($ACCESS_TOKEN == false){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APP_ID."&secret=".$APP_SECRET;
$json = curlSend($url);
$data=json_decode($json,true);
S($APP_ID,$data[access_token],7000);
$ACCESS_TOKEN = S($APP_ID);
} return $ACCESS_TOKEN;
} /**
* 微信网页JSSDK 调用接口获取 $jsapi_ticket
* 微信缓存 7200 秒,这里使用thinkphp的缓存方法
* @param unknown $ACCESS_TOKEN
* @return Ambigous <mixed, Thinkmixed, object>
*/
function get_jsapi_ticket($ACCESS_TOKEN){
$jsapi_ticket = S($ACCESS_TOKEN);
if($jsapi_ticket == false){
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$ACCESS_TOKEN."&type=jsapi";
$json = curlSend($url);
$data = json_decode($json,true); S($ACCESS_TOKEN,$data[ticket],7000);
$jsapi_ticket = S($ACCESS_TOKEN);
}
return $jsapi_ticket;
} /**
* 微信网页JSSDK 获取签名字符串
* 所有参数名均为小写字符
* @param unknown $nonceStr 随机字符串
* @param unknown $timestamp 时间戳
* @param unknown $jsapi_ticket
* @param unknown $url 调用JS接口页面的完整URL,不包含#及其后面部分
*/
function get_js_sdk($APP_ID,$APP_SECRET){
$protocol = (!empty($_SERVER[HTTPS]) && $_SERVER[HTTPS] !== off || $_SERVER[SERVER_PORT] == 443) ? "https://" : "http://";
$url = $protocol.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI]; $argu = array();
$argu[appId] = $APP_ID;
$argu[url] = $url;
$argu[nonceStr] = createNonceStr();
$argu[timestamp] = time(); $ACCESS_TOKEN = get_accesstoken($APP_ID, $APP_SECRET);
$argu[jsapi_ticket] = get_jsapi_ticket($ACCESS_TOKEN); $string = "jsapi_ticket=".$argu[jsapi_ticket]."&noncestr=".$argu[nonceStr]."×tamp=".$argu[timestamp]."&url=".$argu[url];
$argu[signature] = sha1(trim($string));
return $argu;
} /**
* 获取随机字符串
* @param number $length
* @return string
*/
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;
}
其次在程序里面调用JSSDK的配置信息,如下:
$confinfo = get_js_sdk("你的APPID","你的APP_SECRET");
$this->assign("confinfo",$confinfo);
最后创建html文件,其中JS内容如下:
//引入微信JS支持文件//=============这里博主用的是SMARTY引擎,所以代码自然就是smarty代码 var timestamp = "{$confinfo[timestamp]}"; var nonceStr = "{$confinfo[nonceStr]}"; var signature = "{$confinfo[signature]}"; var appId = "{$confinfo[appId]}"; /*******以下JS仅仅在微信浏览器下有效*********/ wx.config({ debug:false, appId:appId, timestamp:timestamp, nonceStr:nonceStr, signature:signature, jsApiList:[onMenuShareTimeline,onMenuShareAppMessage,onMenuShareQQ] }); wx.ready(function(){ 这个地方就是你的分享代码了,具体您可以去微信官网看看那个让人蛋疼的JSSDK文档吧,最主要的内容在上面,仔细看哦! });