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 | 百度智能云文档

相关推荐
学Linux的语莫15 分钟前
python中类的常见写法
开发语言·python·状态模式
需要82617 分钟前
容器化 Java:镜像瘦身与 JVM 容器感知(cgroup)
java·开发语言·jvm
dyxal32 分钟前
A2A 协议 v1.0 完全指南:方法名、Agent Card、三种绑定与 FastAPI 实战
开发语言·qt·fastapi
譕痕36 分钟前
Rabbit 实例
java·开发语言·java-rabbitmq
综合简报1 小时前
黑五海外发稿投给谁?三类海外媒体发稿渠道的分工
开发语言·php·媒体
大风起兮云飞扬丶1 小时前
Java——线程池
java·开发语言
我命由我123452 小时前
Python 开发 - Python 虚拟环境
运维·开发语言·后端·python·http·https·java-ee
c#上位机2 小时前
C#上位机项目实战——exe项目调试更改dll中源代码
开发语言·c#
basketball6162 小时前
C++ 中字符串与常见类型的转换总结
开发语言·c++
梦影_2 小时前
MySQL进阶教程(一)—— 储存过程与触发器(上)
开发语言·数据库·后端·sql·mysql·中间件