admin管理员组

文章数量:1632045

网上源码挺多的,但是感觉暴露自己的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接口,需要的可以自行修改

还是放张截图吧

本文标签: 实时使用量PHPapikeychatGPT