华为公有云API认证部分

有选择谁会用华为云。。。

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";
}

?>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值