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