这个是在ci项目中,用到的微信分享的一个类,储存用的redis
/** * wechart service * 微信service * * @author guomumin<aaron8573@gmail.com> * @date 2016-03-21 */ class Wechart { /** * 微信存储到redis中的access_token key * @var string */ const ACCESS_TOKEN_KEY = 'wechart_access_token'; /** * 微信存储到redis中的jsapi_ticket key * @var string */ const JSAPI_TICKET_KEY = 'wechart_jsapi_ticket'; /** * 微信标识 * @var string */ private $_app_id = NULL; /** * 微信秘钥 * @var string */ private $_app_secret = NULL; /** * 微信授权秘钥 * @var string */ private $_accress_token = NULL; /** * 微信jsapi * @var string */ private $_api_ticket = NULL; /** * 微信授权地址 * @var string */ private $_token_url = 'https://api.weixin.qq.com/cgi-bin/token'; /** * jsapi获取ticket地址 * @var string */ private $_ticket_url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket'; public function __construct() { parent::__construct(); $this->_app_id = ''; //你的app_id $this->_app_secret = ''; //你的app_secret $this->load->driver('cache'); } /** * 获取access_token * @return WechartService */ public function getToken() { $token_info = $this->cache->redis->get(self::ACCESS_TOKEN_KEY); if (empty($token_info)) { $query = ['grant_type' => 'client_credential', 'appid' => $this->_app_id, 'secret' => $this->_app_secret]; $token_url = $this->_token_url.'?'.http_build_query($query); $response = curlSend($token_url, NULL, 'get'); if (isJson($response['result'])) { $token_info = json_decode($response['result'], true); if (isset($token_info['errcode'])) { // 返回结果中没有access_token return $this; } $token_info['expires_in'] = time() + $token_info['expires_in'] - 200; $this->cache->redis->save(self::ACCESS_TOKEN_KEY, json_encode($token_info), 7000); }else { // 请求返回结果不是json 这种情况概率非常低,除非微信服务器挂了 return $this; } }else { $token_info = json_decode($token_info, true); //看看access_token是否过期 if ($token_info['expires_in'] <= time()) { $this->cache->redis->delete(self::ACCESS_TOKEN_KEY); return $this->getToken(); } } $this->_accress_token = $token_info['access_token']; return $this; } /** * 获取api_ticket * @return WechartService */ public function getTicket() { $ticket_info = $this->cache->redis->get(self::JSAPI_TICKET_KEY); if (empty($ticket_info)) { $query = ['access_token' => $this->_accress_token, 'type' => 'jsapi']; $url = $this->_ticket_url.'?'. http_build_query($query); $response = curlSend($url, NULL, 'get'); if (isJson($response['result'])) { $ticket_info = json_decode($response['result'], true); if ($ticket_info['errcode'] != 0) { // 返回结果中没有access_token return $this; } $ticket_info['expires_in'] = time() + $ticket_info['expires_in'] - 200; $this->cache->redis->save(self::JSAPI_TICKET_KEY, json_encode($ticket_info), 7000); }else{ // 请求返回结果不是json 这种情况概率非常低,除非微信服务器挂了 return $this; } }else { $ticket_info = json_decode($ticket_info, true); //看看access_token是否过期 if ($ticket_info['expires_in'] <= time()) { $this->cache->redis->delete(self::JSAPI_TICKET_KEY); return $this->getTicket(); } } $this->_api_ticket = $ticket_info['ticket']; return $this; } /** * 获取分享信息 * @return multitype:NULL string Ambigous <string, number, Ambigous <string, mixed>> */ public function getSharInfo() { $this->getToken()->getTicket(); $noncestr = randNum(16, 9); $url = (isHttps() ? 'https://' : 'http://' ).$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; //注意param中key排列顺序,需按 ASCII 码从小到大排序 $params = [ 'jsapi_ticket' => $this->_api_ticket, 'noncestr' => $noncestr, 'timestamp' => time(), 'url' => $url, ]; ksort($params, SORT_STRING); $string = http_build_query( $params ); $string = urldecode( $string ); $signature = sha1( $string ); $data = ['appId'=>$this->_app_id, 'timestamp'=>$params['timestamp'], 'nonceStr'=>$noncestr, 'signature'=>$signature]; return $data; } /* 是否是微信环境 * * return bool */ public function isWeixin(){ $agent = strtolower($_SERVER['HTTP_USER_AGENT']); $is_weixin = (stripos($agent, 'micromessenger') !== false); return $is_weixin; } }