/** * 依赖于数据库2小时缓存获取token,ticket * @param $appid * @param $secret * @return array 包含token,ticket的数组 */ function db_get_token_ticket($appid, $secret) { $token = " "; $ticket = " "; $params = array(array($appid, SQLSRV_PARAM_IN), array($token, SQLSRV_PARAM_OUT), array($ticket, SQLSRV_PARAM_OUT)); sp_execute("{call weixin_get_token(?,?,?)}", $params); if (strlen($token) == 0 || strlen($ticket) == 0) { $token = get_access_token($appid, $secret); $ticket = get_jsapi_ticket($token); $params = array(array($appid, SQLSRV_PARAM_IN), array($token, SQLSRV_PARAM_IN), array($ticket, SQLSRV_PARAM_IN)); sp_execute("{call weixin_set_token(?,?,?)}", $params); } return array("token" => $token, "ticket" => $ticket); }
public function getSignPackage() { $jsapiTicket = get_jsapi_ticket(); //获取ticket $url = get_current_url(); //获取当前url $timestamp = time(); $nonceStr = $this->_createNonceStr(); // 这里参数的顺序要按照 key 值 ASCII 码升序排序 $string = "jsapi_ticket=" . $jsapiTicket . "&noncestr=" . $nonceStr . "×tamp=" . $timestamp . "&url=" . $url; $signature = sha1($string); $signPackage = array("appId" => $this->appId, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string); return $signPackage; }
public function getSignPackage() { //获取ticket,有缓存功能。 $jsapiTicket = get_jsapi_ticket(); // 注意 URL 一定要动态获取,不能 hardcode. $protocol = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://"; $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("appId" => $this->appId, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string); return $signPackage; }
$modules[$i]['desc'] = 'refresh_wxtoken_desc'; /* 作者 */ $modules[$i]['author'] = 'gy.wang'; /* 网址 */ $modules[$i]['website'] = ''; /* 版本号 */ $modules[$i]['version'] = '0.1.0'; /* 配置信息 */ $modules[$i]['config'] = array(array('name' => 'minuts_interval', 'type' => 'text', 'value' => '')); return; } $wx = $db->getRow("SELECT * FROM `wxch_config` WHERE `id`=1"); if (!empty($wx) && $wx['dateline'] <= $timestamp) { $tk = get_access_token($wx['appid'], $wx['appsecret']); if (!empty($tk)) { $jsticket = get_jsapi_ticket($tk['access_token']); $expire = $timestamp + intval($tk['expires_in']) - 900; $db->query("UPDATE `wxch_config` SET access_token='{$tk[access_token]}', jsapi_token='{$jsticket}',dateline='{$expire}' WHERE `id`=1"); //write to cache file $wxtk = array('token' => $wx['token'], 'appid' => $wx['appid'], 'appsecret' => $wx['appsecret'], 'access_token' => $tk['access_token'], 'jsapi_token' => $jsticket, 'expire' => $expire); write_config('wxtoken', $wxtk); } } elseif (!file_exists(ROOT_PATH . '/data/wxtoken.php')) { $wxtk = array('token' => $wx['token'], 'appid' => $wx['appid'], 'appsecret' => $wx['appsecret'], 'access_token' => $wx['access_token'], 'jsapi_token' => $wx['jsapi_token'], 'expire' => $wx['dateline']); write_config('wxtoken', $wxtk); } // copy to mobile dir $src = ROOT_PATH . '/data/wxtoken.php'; $dest = ROOT_PATH . '/mobile/data/wxtoken.php'; @copy($src, $dest); // put to memcache
<?php ini_set("display_errors", 1); $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $access_token = get_access_token($redis); $jsapi_ticket = get_jsapi_ticket($redis, $access_token); function get_access_token($redis) { $access_token = $redis->get('wechat_access_token'); $access_token = json_decode($access_token); $expires = $redis->get('wechat_access_token_expires'); if (isset($access_token->access_token)) { return $access_token->access_token; } else { $appid = 'wx131b06b8cdbe4111'; $appsecret = 'd895a6f60e5cd1abaa93f513d57d34bc'; $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret; $data = file_get_contents($url); $redis->set('wechat_access_token', $data, 3600); $json = json_decode($data); return $json->access_token; } } function get_jsapi_ticket($redis, $access_token) { $ticket = $redis->get('wechat_jsapi_ticket'); $ticket = json_decode($ticket); $expires = $redis->get('wechat_jsapi_expires'); if (isset($ticket->ticket)) { return $ticket->ticket;