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

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

请求结构

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

响应参数

名称类型描述
cashBalanceBigDecimal用户余额

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

  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值