//测试 $APPID=‘*************************‘; $SECRET=‘***********************‘; $url = trim($_REQUEST[‘url‘]); $root[‘url‘] = $url; $memcache = getRedis(); $mem_valid_time = 7100; //获取access_token,并缓存 $memcache_key = ‘wx_share_sign_access_token‘; $mem_list = $memcache->get($memcache_key); //缓存中没有数据 if (empty($mem_list)) { $token = null; if (!$token || strlen($token) < 6) { $res = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$SECRET.""); $res = json_decode($res, true); $token = $res[‘access_token‘]; } $new_list = $token; //更新缓存 $memcache->set($memcache_key,$new_list,$mem_valid_time); } else { $token = $mem_list;//缓存中的数据 } $root[‘access_token‘] = $token; //获取jsapi_ticket,并缓存 $memcache_key = ‘sign_jsapi_ticket‘; $mem_list = $memcache->get($memcache_key); //缓存中没有取数据库 if (empty($mem_list)) { $jsapi_ticket = null; if (!$jsapi_ticket || strlen($jsapi_ticket) < 6) { $ur = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$token}&type=jsapi"; $res = file_get_contents($ur); $res = json_decode($res, true); $jsapi_ticket = $res[‘ticket‘]; } $new_list = $jsapi_ticket; //更新缓存 $memcache->set($memcache_key,$new_list,$mem_valid_time); } else { $jsapi_ticket = $mem_list;//缓存中的数据 } $root[‘jsapi_ticket‘] = $jsapi_ticket; $timestamp = time();//生成签名的时间戳 $metas = range(0, 9); $metas = array_merge($metas, range(‘A‘, ‘Z‘)); $metas = array_merge($metas, range(‘a‘, ‘z‘)); $nonceStr = ‘‘; for ($i=0; $i < 16; $i++) { $nonceStr .= $metas[rand(0, count($metas)-1)];//生成签名的随机串 } $string1="jsapi_ticket=".$jsapi_ticket."&noncestr=".$nonceStr."×tamp=".$timestamp."&url=".$url.""; $signature=sha1($string1); $root[‘appid‘] = $APPID; $root[‘nonceStr‘] = $nonceStr; $root[‘timestamp‘] = $timestamp; $root[‘signature‘] = $signature;
return $tag;