php-微信分享签名
程序员文章站
2022-03-21 17:23:02
//测试 $APPID='*************************'; $SECRET='***********************'; $url = trim($_REQUEST['url']); $root['url'] = $url; $memcache = getRedis() ......
//测试 $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;
上一篇: Mac 停止redis服务