php 调用阿里云API

//入口
public function index()
{
    date_default_timezone_set("GMT");
    $Timestamp = date('Y-m-d\TH:i:s\Z',time());
    date_default_timezone_set("PRC");
    $action = 'DescribeRegions';
    $accessKeyId = 'XXXXXXX';
    $signatureMethod = 'HMAC-SHA1';
    $signatureVersion = '1.0';
    $signatureNonce = $this->getRandStr(6,1);
    $version = '2014-05-26';
    $format = 'json';
    $secret_key = 'XXXXXXXXX';
    $public =
        [
            'Action'    =>  $action,
            'AccessKeyId'   =>  $accessKeyId,
            'SignatureMethod'   =>  $signatureMethod,
            'SignatureVersion'  =>  $signatureVersion,
            'SignatureNonce'    =>  $signatureNonce,
            'Timestamp' =>  $Timestamp,
            'Version' =>    $version,
            'Format'    =>  $format
        ];
    ksort($public);
    $arr = [];
    foreach ($public as $key => $val)
    {
        $arr[] = $this->percentEncode($key).'='.$this->percentEncode($val);
    }
    $str = join('&',$arr);
    $stringToSign = 'POST&%2F&'.$this->percentEncode($str);
    $signature = base64_encode(hash_hmac('sha1',utf8_encode($stringToSign),$secret_key.'&',true));
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "http://ecs.aliyuncs.com/?$str&Signature=".$signature);
    //设置post方式提交
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $tmpInfo = curl_exec($curl);

    print_r($tmpInfo);

}
public function getRandStr($length,$num = 0)
{
    $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    if($num == 1)
    {
        $str = '0123456789';
    }
    $len = strlen($str)-1;
    $rand_str = '';
    for($i = 0 ;$i < $length ;++$i)
    {
        $num = mt_rand(0,$len);
        $rand_str .=$str[$num];
    }
    return $rand_str;
}
public  function percentEncode($str)
{
    $res = urlencode($str);
    $res = preg_replace('/\+/', '%20', $res);
    $res = preg_replace('/\*/', '%2A', $res);
    $res = preg_replace('/%7E/', '~', $res);
    return $res;
}

结果:

{
    "RequestId": "XXXXXXX",
    "Regions": {
        "Region": [
            {
                "RegionId": "cn-qingdao",
                "RegionEndpoint": "ecs.aliyuncs.com",
                "LocalName": "华北 1"
            },
            {
                "RegionId": "cn-beijing",
                "RegionEndpoint": "ecs.aliyuncs.com",
                "LocalName": "华北 2"
            },
            {
                "RegionId": "cn-zhangjiakou",
                "RegionEndpoint": "ecs.cn-zhangjiakou.aliyuncs.com",
                "LocalName": "华北 3"
            },
            {
                "RegionId": "cn-huhehaote",
                "RegionEndpoint": "ecs.cn-huhehaote.aliyuncs.com",
                "LocalName": "华北 5"
            },
            {
                "RegionId": "cn-hangzhou",
                "RegionEndpoint": "ecs.aliyuncs.com",
                "LocalName": "华东 1"
            },
            {
                "RegionId": "cn-shanghai",
                "RegionEndpoint": "ecs.aliyuncs.com",
                "LocalName": "华东 2"
            },
            {
                "RegionId": "cn-shenzhen",
                "RegionEndpoint": "ecs.aliyuncs.com",
                "LocalName": "华南 1"
            },
            {
                "RegionId": "cn-hongkong",
                "RegionEndpoint": "ecs.aliyuncs.com",
                "LocalName": "香港"
            },
            {
                "RegionId": "ap-northeast-1",
                "RegionEndpoint": "ecs.ap-northeast-1.aliyuncs.com",
                "LocalName": "亚太东北 1 (东京)"
            },
            {
                "RegionId": "ap-southeast-1",
                "RegionEndpoint": "ecs.aliyuncs.com",
                "LocalName": "亚太东南 1 (新加坡)"
            },
            {
                "RegionId": "ap-southeast-2",
                "RegionEndpoint": "ecs.ap-southeast-2.aliyuncs.com",
                "LocalName": "亚太东南 2 (悉尼)"
            },
            {
                "RegionId": "ap-southeast-3",
                "RegionEndpoint": "ecs.ap-southeast-3.aliyuncs.com",
                "LocalName": "亚太东南 3 (吉隆坡)"
            },
            {
                "RegionId": "ap-southeast-5",
                "RegionEndpoint": "ecs.ap-southeast-5.aliyuncs.com",
                "LocalName": "亚太东南 5 (雅加达)"
            },
            {
                "RegionId": "ap-south-1",
                "RegionEndpoint": "ecs.ap-south-1.aliyuncs.com",
                "LocalName": "亚太南部 1 (孟买)"
            },
            {
                "RegionId": "us-east-1",
                "RegionEndpoint": "ecs.aliyuncs.com",
                "LocalName": "美国东部 1 (弗吉尼亚)"
            },
            {
                "RegionId": "us-west-1",
                "RegionEndpoint": "ecs.aliyuncs.com",
                "LocalName": "美国西部 1 (硅谷)"
            },
            {
                "RegionId": "me-east-1",
                "RegionEndpoint": "ecs.me-east-1.aliyuncs.com",
                "LocalName": "中东东部 1 (迪拜)"
            },
            {
                "RegionId": "eu-central-1",
                "RegionEndpoint": "ecs.eu-central-1.aliyuncs.com",
                "LocalName": "欧洲中部 1 (法兰克福)"
            }
        ]
    }
}

猜你喜欢

转载自blog.csdn.net/landylxy/article/details/81220865
今日推荐