全部文档
当前文档

暂无内容

如果没有找到您期望的内容,请尝试其他搜索词

文档中心

PHP

最近更新时间:2024-08-12 10:30:24

PHP签名DEMO如下:

<?php

//简易签名函数,使用HMAC-SHA256算法
function sign($params, $secretKey) {
    //对参数键进行排序
    ksort($params);

    //构建待签名字符串
    $strEncode = '';
    foreach ($params as $key => $value) {
        $strEncode .= rawurlencode($key) . '=' . rawurlencode($value) . '&';
    }
    $strEncode = substr($strEncode, 0, -1);

    //生成HMAC-SHA256签名
    $hmac = hash_hmac('sha256', $strEncode, $secretKey, true);
    return bin2hex($hmac);
}

$ak = "Your AK"; // 请替换为您的AccessKey
$sk = "Your SK"; // 请替换为您的SecretKey
$service = "iam"; // 访问的服务
$region = "cn-beijing-6"; // 机房

$params = [
    // 固定参数
    "Accesskey" => $ak,
    "Service" => $service,
    "Region" => $region,
    "Action" => "ListUsers", // 接口名
    "Version" => "2015-11-01", // 接口版本号
    "Timestamp" => gmdate("Y-m-d\TH:i:s\Z"),
    "SignatureVersion" => "1.0",
    "SignatureMethod" => "HMAC-SHA256",

    // 接口参数
    "Marker" => "1",
    "MaxItems" => "100",
];

$signature = sign($params, $sk);

$requestURL = sprintf("http://%s.api.ksyun.com/?", $service) . http_build_query($params) . "&Signature=" . $signature;

$headers = [
    "Accept: application/json",
    "Host: " . sprintf("%s.api.ksyun.com", $service),
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $requestURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "Response Status: " . $httpCode . "\n";
echo "Response Body: " . $response . "\n";

文档导读
纯净模式常规模式

纯净模式

点击可全屏预览文档内容
文档反馈