当前位置:   article > 正文

【PHP】利用apikey获取ChatGPT的实时使用量_查看chatapi次数

查看chatapi次数

网上源码挺多的,但是感觉暴露自己的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;
}
?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

这个代码我没有特意写json接口,需要的可以自行修改

还是放张截图吧
本地搭建,无外网环境

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/970642
推荐阅读
相关标签
  

闽ICP备14008679号