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

相关推荐
tatasix9 分钟前
Go Redis实现排行榜
开发语言·redis·golang
吴冰_hogan12 分钟前
Java虚拟机(JVM)的类加载器与双亲委派机制
java·开发语言·jvm
程序员shen16161113 分钟前
注意⚠️:矩阵系统源码开发/SaaS矩阵系统开源/抖音矩阵开发优势和方向
java·大数据·数据库·python·php
菜鸟xiaowang15 分钟前
Android.bp java_library_static srcs配置
开发语言·python
怎么就重名了17 分钟前
MAC M4安装QT使用国内镜像源在线安装
开发语言·qt
Evand J18 分钟前
一维、线性卡尔曼滤波的例程(MATLAB)
开发语言·matlab
pl002026 分钟前
C++运算符重载实例
开发语言·c++·运算符重载·单目运算符·双目运算符·流运算符
煤泥做不到的!33 分钟前
挑战一个月基本掌握C++(第十二天)了解命名空间,模板,预处理器
开发语言·c++
青春男大36 分钟前
java队列--数据结构
java·开发语言·数据结构·学习·eclipse
shaoing38 分钟前
php的zip扩展 先装libzip
php