admin管理员组文章数量:1631879
网上源码挺多的,但是感觉暴露自己的key到其他地方还是不够安全
于是打算开源这个PHP的代码,方便大家自己部署
话也不多说,直接上代码,喜欢的码一个,不喜欢的,自便,谢谢大家
<?php
header('Content-Type: text/html; charset=UTF-8');
error_reporting(0);
//$httpcode = 0;
$start_date = date('Y-m-d', strtotime('-99 days'));
$end_date = date('Y-m-d', strtotime('+1 day'));
$apikey = "sk-*******************************************************";
$suburl = "https://api.***.***/v1/dashboard/billing/subscription";
$dataurl = "https://api.***.***/v1/dashboard/billing/usage?start_date=".$start_date."&end_date=".$end_date;
$headers = [
'Content-Type: application/json',
'Authorization: Bearer ' . $apikey
];
$resdata = curl_get($suburl,$headers);
if ($httpcode == 200) {
$json_a = json_decode($resdata, true);
$billing = $json_a['hard_limit_usd'];
$data = curl_get($dataurl,$headers);
$json_a = json_decode($data, true);
$used = round($json_a['total_usage']) / 100;
$free = $billing - $used;
echo "额度总量: " . $billing . " 美元" . '<br/>';
echo "已用额度: " . $used . " 美元" . '<br/>';
echo "剩余额度: " . $free . " 美元";
} elseif ($httpcode == 0) {
echo "无法访问接口地址<br/>请检查接口地址是否错误";
} else {
echo "接口访问错误<br/>返回状态码: " . $httpcode;
}
function curl_get ($url,$headarr) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headarr);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
$data = curl_exec($ch);
$GLOBALS['httpcode'] = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
return $data;
}
?>
这个代码我没有特意写json接口,需要的可以自行修改
还是放张截图吧
版权声明:本文标题:【PHP】利用apikey获取ChatGPT的实时使用量 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1729103389a1186765.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论