<?php

function SetAccess_Token()
{
    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx4eab2e3b5531d58b&secret=1026580a52dc89f564395113017c165b';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}

function GetAccess_Token()
{
    $User = M('wc_access_token');
    $time = $User->where('id=1')->field('update_time,access_token')->select();
    $s = floor(strtotime(date('Y-m-d H:i:s')) - strtotime($time[0]['update_time']));
    if ($s > 3600) {
        $token = SetAccess_Token();
        $access = json_decode($token, true);
        $User->where('id=1')->save(array('access_token' => $access['access_token'], 'update_time' => date('Y-m-d H:i:s')));
        return $access['access_token'];
    } else {
        return $time[0]['access_token'];
    }
}


function GetAccess_Token1()
{
    $User = M('wc_access_token');
//    $time = $User->where('id=1')->field('update_time,access_token')->select();
    $token = SetAccess_Token();
    $access = json_decode($token, true);
    $User->where('id=1')->save(array('access_token' => $access['access_token'], 'update_time' => date('Y-m-d H:i:s')));
    return $access['access_token'];

}

?>