php-微信共有署名

8975 ワード

// 
$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."&timestamp=".$timestamp."&url=".$url."";
$signature=sha1($string1);
$root['appid'] = $APPID;
$root['nonceStr'] = $nonceStr;
$root['timestamp'] = $timestamp;
$root['signature'] = $signature;

return $tag
;