PHP code example of zhuyl / aliyuntoken

1. Go to this page and download the library: Download zhuyl/aliyuntoken library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zhuyl / aliyuntoken example snippets



re_once (__DIR__.DIRECTORY_SEPARATOR.'Cache.php');

$AccessKey ='<您从阿里云获得的AccessKeyId>';
$AccessKeySecret ='<您从阿里云获得的AccessKeySecret>';
try{
    $obj=new \Zhuyl\AliyunToken($AccessKey,$AccessKeySecret);
    $token=$obj->getToken();        //接口获取最新token
    $cache=new \Zhuyl\Cache();      //实例化缓存类(如果不使用缓存也去除以下3行代码)
    $cache->put('token',$token);    //写入缓存
    $token=$cache->get('token');    //获取缓存信息
    print_r($token);
    print_r($token['Id']);
}catch (Exception $e){
    die($e->getMessage());
}

array (
  'UserId' => 'xxxxxxxxxxxxxxxxxx',                 //阿里云账号ID
  'Id' => 'cc0c8b52a86440e5ae70ac75da1d4d35',       //请求后分配的Token值
  'ExpireTime' => 1635094339,                       //Token的有效期时间戳(单位:秒。例如1553825814换算为北京时间为:2019/3/29 10:16:54,即Token在该时间之前有效。)
)

/*
 * 实例化缓存
 * @var $exp_time   int     缓存过期时间(默认3600秒)
 * @var $path       string  存储缓存文件的路径
 */
$cache=new \Zhuyl\Cache($exp_time = 3600, $path = __DIR__.DS."runtime".DS."cache".DS);
$cache->put('token',$token);    //写入缓存
$token=$cache->get('token');    //获取缓存信息,成功返回数据,失败返回false
$token=$cache->del('token');    //删除缓存信息,成功返回true,失败返回false