有选择谁会用华为云。。。
config.php
<?php
$username = "xxxxxxxxxxxxxxxxxx";
$password = "xxxxxxxxxxxxxxxxxxxx";
$domainName = "xxxxxxxxxxxxxxxxxxx";
$projectId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$iamUrl = "https://iam.cn-north-1.myhuaweicloud.com/v3/auth/tokens";
$vpcUrl = "https://vpc.cn-north-1.myhuaweicloud.com/v1/".$projectId;
$ecsUrl = "https://ecs.cn-north-1.myhuaweicloud.com/v1/".$projectId;
>
auth.php
<?php
#include "./config.php";
require("./config.php");
function getResponseHeaderArray($rh){
$rh = explode("\n",$rh);
$responseArray = [];
foreach($rh as $rso){
$list = explode(":", $rso);
$responseArray[$list[0]] = $list[1];
}
return $responseArray["X-Subject-Token"];
}
function myCurl($url){
global $token;
$requestHeader = array(
"Content-Type: application/json",
"X-Auth-Token:".$token,
);
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
#curl_setopt($ch,CURLOPT_POST,1);
#curl_setopt($ch,CURLOPT_POSTFIELDS,$postJson);
curl_setopt($ch,CURLOPT_HTTPHEADER, $requestHeader);
$output = curl_exec($ch);
return $output;
}
function hwAuth(){
global $username;
global $password;
global $domainName;
global $projectId;
global $iamUrl;
$requestHeader = array(
"Content-Type: application/json"
);
$postArray = array(
"auth" => array(
"identity" => array(
"methods" => array("password"), "password" => array(
"user" => array(
"name"=> $username, "password" => $password, "domain" => array(
"name" => $domainName
)
)
)
),
"scope" => array(
"project" => array(
"id" => $projectId
)
)
)
);
$postJson = json_encode($postArray);
$ch = curl_init($iamUrl);
curl_setopt($ch,CURLOPT_HEADER,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postJson);
curl_setopt($ch,CURLOPT_HTTPHEADER, $requestHeader);
$output = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$responseHeader = substr($output, 0, $headerSize);
$responseBody = substr($output, $headerSize);
curl_close($ch);
//var_dump($output);
$token = getResponseHeaderArray($responseHeader);
$token_expires_at = strtotime(json_decode($responseBody)->token->expires_at);
//echo $token_expires_at." ".$token;
file_put_contents("./file/token.txt",$token_expires_at." ".$token);
}
$tokenFile = file_get_contents("./file/token.txt");
list($token_expires_at,$token) = explode(" ",$tokenFile);
if(time() > $token_expires_at-600 ){
hwAuth();
$tokenFile = file_get_contents("./file/token.txt");
list($token_expires_at,$token) = explode(" ",$tokenFile);
echo "reAuth"."\n";
}
?>