去github上下載阿里phpsdk
<?php
header('Access-Control-Allow-Origin:*');
require_once './plugin/aliyun-openapi-php-sdk/aliyun-php-sdk-core/Config.php';
require_once './plugin/aliyun-openapi-php-sdk/aliyun-php-sdk-nls-cloud-meta/nls_cloud_meta/Request/V20180518/CreateTokenRequest.php';
use nls_cloud_meta\Request\V20180518\CreateTokenRequest;
DefaultProfile::addEndpoint(
"cn-shanghai",
"cn-shanghai",
"nls-cloud-meta",
"nls-meta.cn-shanghai.aliyuncs.com");
# 創(chuàng)建DefaultAcsClient實(shí)例并初始化
$clientProfile = DefaultProfile::getProfile(
"cn-", # Region ID
"", # 您的 AccessKey ID
"" # 您的 AccessKey Secret
);
$zero1=date("Y/m/d");
$myfile = fopen("TokenTime.txt", "r") or die("Unable to open file!");
$zero2= fread($myfile,filesize("TokenTime.txt"));
fclose($myfile);
if(strtotime($zero1)>strtotime($zero2)){
$myfile = fopen("TokenTime.txt", "w") or die("Unable to open file!");
$txt =date("Y/m/d");
fwrite($myfile, $txt);
fclose($myfile);
$client = new DefaultAcsClient($clientProfile);
# 創(chuàng)建API請(qǐng)求并設(shè)置參數(shù)
$request = new CreateTokenRequest();
# 發(fā)起請(qǐng)求并處理返回
try {
$response = $client->getAcsResponse($request);
$txt1 =$response->Token;
print_r($txt1);
$myfile1 = fopen("Token.txt", "w") or die("Unable to open file!");
fwrite($myfile1,json_encode($txt1, JSON_FORCE_OBJECT));
fclose($myfile1);
} catch(ServerException $e) {
print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
} catch(ClientException $e) {
print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
}
}else{
$myfile1 = fopen("Token.txt", "r") or die("Unable to open file!");
$token= fread($myfile1,filesize("Token.txt"));
fclose($myfile1);
print_r(json_decode($token));
}
?>
阿里oss的使用需要先去后臺(tái)創(chuàng)建角色和用戶,使用的sdk和語(yǔ)音識(shí)別一樣。
<?php
/*
* 在您使用STS SDK前,請(qǐng)仔細(xì)閱讀RAM使用指南中的角色管理部分,并閱讀STS API文檔
*
*/
include_once './aliyun-php-sdk-core/Config.php';
include'Sts/Request/V20150401/AssumeRoleRequest.php';
use Sts\Request\V20150401 as Sts;
define("REGION_ID", "cn-hangzhou");
define("ENDPOINT", "sts.cn-hangzhou.aliyuncs.com");
// 只允許子用戶使用角色
DefaultProfile::addEndpoint(REGION_ID, REGION_ID, "Sts", ENDPOINT);
$iClientProfile = DefaultProfile::getProfile(REGION_ID, "", "");
$client = new DefaultAcsClient($iClientProfile);
// 角色資源描述符,在RAM的控制臺(tái)的資源詳情頁(yè)上可以獲取
$roleArn = "";
// 在扮演角色(AssumeRole)時(shí),可以附加一個(gè)授權(quán)策略,進(jìn)一步限制角色的權(quán)限;
// 詳情請(qǐng)參考《RAM使用指南》
// 此授權(quán)策略表示讀取所有OSS的只讀權(quán)限
//$policy=<<<POLICY
//{
// "Statement": [
// {
// "Action": [
// "oss:Get*",
// "oss:List*"
// ],
// "Effect": "Allow",
// "Resource": "*"
// }
// ],
// "Version": "1"
//}
//POLICY;
$request = new Sts\AssumeRoleRequest();
// RoleSessionName即臨時(shí)身份的會(huì)話名稱(chēng),用于區(qū)分不同的臨時(shí)身份
// 您可以使用您的客戶的ID作為會(huì)話名稱(chēng)
$request->setRoleSessionName("client_name");
$request->setRoleArn($roleArn);
//$request->setPolicy($policy);
$request->setDurationSeconds(3600);
try {
$response = $client->getAcsResponse($request);
print_r($response);
} catch(ServerException $e) {
print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
} catch(ClientException $e) {
print "Error: " . $e->getErrorCode() . " Message: " . $e->getMessage() . "\n";
}
?>