[PHP]火山引擎Header签名以及接口调用函数

火山引擎 通用文生图V2.0 PHP签名

函数


    public function request($Service, $Version, $Region, $Host, $ContentType, $method, $query, $header, $action, $body)
    {

        // 第二步:创建身份证明。其中的 Service 和 Region 字段是固定的。ak 和 sk 分别代表
        // AccessKeyID 和 SecretAccessKey。同时需要初始化签名结构体。一些签名计算时需要的属性也在这里处理。
        // 初始化身份证明结构体
        $ak = $this->access_key;
        $sk = $this->secret_key;
        $credential = [
            'accessKeyId' => $ak,
            'secretKeyId' => $sk,
            'service' => $Service,
            'region' => $Region,
        ];

        // 初始化签名结构体
        $query = array_merge($query, [
            'Action' => $action,
            'Version' => $Version
        ]);
        ksort($query);
        $requestParam = [
            // body是http请求需要的原生body
            'body' => $body,
            'host' => $Host,
            'path' => '/',
            'method' => $method,
            'contentType' => $ContentType,
            'date' => gmdate('Ymd\THis\Z'),
            'query' => $query
        ];

        // 第三步:接下来开始计算签名。在计算签名前,先准备好用于接收签算结果的 signResult 变量,并设置一些参数。
        // 初始化签名结果的结构体
        $xDate = $requestParam['date'];
        $shortXDate = substr($xDate, 0, 8);
        $xContentSha256 = hash('sha256', $requestParam['body']);
        $signResult = [
            'Host' => $requestParam['host'],
            'X-Content-Sha256' => $xContentSha256,
            'X-Date' => $xDate,
            'Content-Type' => $requestParam['contentType']
        ];
        // 第四步:计算 Signature 签名。
        $signedHeaderStr = join(';', ['content-type', 'host', 'x-content-sha256', 'x-date']);
        $canonicalRequestStr = join("\n", [
            $requestParam['method'],
            $requestParam['path'],
            http_build_query($requestParam['query']),
            join("\n", ['content-type:' . $requestParam['contentType'], 'host:' . $requestParam['host'], 'x-content-sha256:' . $xContentSha256, 'x-date:' . $xDate]),
            '',
            $signedHeaderStr,
            $xContentSha256
        ]);
        $hashedCanonicalRequest = hash("sha256", $canonicalRequestStr);
        $credentialScope = join('/', [$shortXDate, $credential['region'], $credential['service'], 'request']);
        $stringToSign = join("\n", ['HMAC-SHA256', $xDate, $credentialScope, $hashedCanonicalRequest]);
        $kDate = hash_hmac("sha256", $shortXDate, $credential['secretKeyId'], true);
        $kRegion = hash_hmac("sha256", $credential['region'], $kDate, true);
        $kService = hash_hmac("sha256", $credential['service'], $kRegion, true);
        $kSigning = hash_hmac("sha256", 'request', $kService, true);
        $signature = hash_hmac("sha256", $stringToSign, $kSigning);
        $signResult['Authorization'] = sprintf("HMAC-SHA256 Credential=%s, SignedHeaders=%s, Signature=%s", $credential['accessKeyId'] . '/' . $credentialScope, $signedHeaderStr, $signature);
        $header = array_merge($header, $signResult);
        // 第五步:将 Signature 签名写入 HTTP Header 中,并发送 HTTP 请求。
        $client = new GuzzleHttp\Client([
            'base_uri' => 'https://' . $requestParam['host'],
            'timeout' => 120.0,
            'verify' => false,
        ]);
        return $client->request($method, 'https://' . $requestParam['host'] . $requestParam['path'], [
            'headers' => $header,
            'query' => $requestParam['query'],
            'body' => $requestParam['body']
        ]);
    }


调用方法

request(
  $Service = 'cv',
    $Version = '2024-06-06',
    $Region = 'cn-north-1',
    $Host = 'visual.volcengineapi.com',
    $ContentType = 'application/json',
    $method = "POST",
    $quert = [],
    $header = [],
    $action = 'HighAesGeneralV20L',
    $requestBody
);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

友缘工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值