php百度云账户余额查询API示例

1、官方文档地址:账户余额查询

请求结构

bash 复制代码
POST /v{version}/finance/cash/balance
HTTP/1.1 
Host: billing.baidubce.com 
ContentType: application/json; charset=utf-8
Content-Length: <Content_Length>
Authorization: authorization string

响应参数

名称 类型 描述
cashBalance BigDecimal 用户余额

PHP完整代码示例:

php 复制代码
<?php
//账户余额查询
class Balance {
    
    private $ak;
    private $sk;
    
    public function __construct($ak, $sk)
    {
        $this->ak = $ak;
        $this->sk = $sk;
    
        if (empty($this->ak) || empty($this->sk)) {
            exit('请配置ak 或者 sk');
        }
    
    }
    
    public function getBalance()
    {
        $data = [];
        $res = $this->getBaiduApi($data);
        if (isset($res['cashBalance'])) {
            $result =  [
                'code' => 200,
                'cashBalance' => $res['cashBalance'],
                'msg' => 'ok'
            ];
        } else {
            $result =  [
                'code' => 10001,
                'msg' => $res['message']
            ];
        }
    
        return $result;
    }
    
    public function getBaiduApi($data = [])
    {
        $host = 'billing.baidubce.com'; // 服务
        $uri = '/v1/finance/cash/balance'; // 接口路径
        $startUtc = gmdate("Y-m-d\TH:i:s\Z");  // utc 时间戳
        $Authorization = $this->getBaiDuAuthorization($startUtc, $uri, $host);
        $url = "https://" . $host . $uri;
        $data = json_encode($data,JSON_UNESCAPED_UNICODE);
        $header = [
            'Authorization:'.$Authorization,
            'Host:'.$host,
            'content-type:application/json',
            'x-bce-date:'.$startUtc,
            'Content-Length:'.strlen($data)
        ];
        
        return $this->getBaiDuPostCurl($url, $data, $header);
    }
    
    public function getBaiDuAuthorization($startUtc, $canonicalUri, $host)
    {
        // 签名有效期 (秒)
        $validity = '1800';
    
        //签名头域(signedHeaders):加入签名算法的HTTP头域列表,为认证字符串的中间部分。
        $signedHeaders = 'host;x-bce-date';
        
        $Host = "host:" . $host;

        $BceDate = "x-bce-date:" . urlencode($startUtc);

        $httpRequestMethod = "POST";
        
        //规范请求(canonicalRequest):经过规范化处理后的请求信息,又称待签名串。
        $canonicalCan = '';
        
        $canonicalRequest = $httpRequestMethod . "\n" . $canonicalUri . "\n" . $canonicalCan . "\n" . $Host . "\n" . $BceDate;
    
        // 1: 前缀字符串  由除sk字段外的签名信息生成
        $authStringPrefix = "bce-auth-v1/" . $this->ak . "/" . $startUtc . "/" . $validity;
        // 2: 派生签名密钥 signingKey  signingKey = HMAC-SHA-256-HEX("sk", authStringPrefix)
        $signingKey = bin2hex(hash_hmac('sha256',  $authStringPrefix, $this->sk, true));
        // 3: 签名摘要 signature  signature = HMAC-SHA-256-HEX(signingKey, canonicalRequest)
        $signature = bin2hex(hash_hmac('sha256',  $canonicalRequest, $signingKey, true));
        // 4: 认证字符串 authorization  Authorization = authStringPrefix/signedHeaders/signature
        $Authorization = $authStringPrefix . '/' . $signedHeaders .'/' . $signature;
        
        return $Authorization;
    }
    
    
    public function getBaiDuPostCurl($url, $data = null, $header = null)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_NOBODY, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        if ( !empty($header) ) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        
        $res = curl_exec($ch);
        $errorCode = curl_errno($ch);
        curl_close($ch);
        if(0 !== $errorCode) {
            return false;
        }
        
        return json_decode($res, true);
    }
}

$ak = '109e************************68f1';    //换成自己的Access Key
$sk = 'c5e0*************************e4e';    //换成自己的Secret Key
$balance = new Balance($ak, $sk);
$res = $balance->getBalance();
if ($res['code'] == 200) {
    echo '余额:' . $res['cashBalance'] . '元';
} else {
    echo $res['msg'];
}

?>

如何获取AKSK:如何获取AKSK - 相关参考Reference | 百度智能云文档

相关推荐
JaguarJack2 天前
PHP 的异步编程 该怎么选择
后端·php·服务端
BingoGo2 天前
PHP 的异步编程 该怎么选择
后端·php
JaguarJack2 天前
为什么 PHP 闭包要加 static?
后端·php·服务端
ServBay3 天前
垃圾堆里编码?真的不要怪 PHP 不行
后端·php
用户962377954483 天前
CTF 伪协议
php
BingoGo6 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack6 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端
BingoGo7 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack7 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
JaguarJack7 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端