OpenAI 账户余额查询 - 在线查看剩余信用额度
<div class='mx-auto max-w-7xl px-0'><div class='container justify-center items-center'><div class='content'><h1 class='text-3xl font-semibold text-center mb-6'>OpenAI 账户余额查询</h1><form method='POST' onsubmit='event.preventDefault(); checkOpenAICredit();' class='flex items-center mb-10'><label for='api_key' class='w-28 mr-4'>API 密钥:</label><input type='text' placeholder='请输入您的 API 密钥:sk-' name='api_key' id='api_key' class='border border-gray-400 rounded px-2 py-1 w-full max-w-md focus:outline-none focus:border-blue-500' required><button type='submit' class='bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-6 rounded-full ml-4'><span style='display:inline-block; width: 50px;'>查询</span></button><div id='loader' class='loader ml-4' style='display:none;'></div></form><div id='result'></div></div></div></div><script>
function showLoader() {
document.getElementById('loader').style.display = 'inline-block';
}
<p>function hideLoader() {
document.getElementById('loader').style.display = 'none';
}</p>
<p>function displayResult(data) {
if (data.status === 'success') {
document.getElementById('result').innerHTML = <code><div class='grid grid-cols-3 gap-4'> <div class='grid-item bg-gray-100 p-6 rounded-lg text-center'> <p class='text-2xl font-semibold'>${data.total_granted}</p> <p class='text-gray-600'>总额度</p> </div> <div class='grid-item bg-gray-100 p-6 rounded-lg text-center'> <p class='text-2xl font-semibold'>${parseFloat(data.total_used).toFixed(3)}</p> <p class='text-gray-600'>已使用额度</p> </div> <div class='grid-item bg-gray-100 p-6 rounded-lg text-center'> <p class='text-2xl font-semibold'>${parseFloat(data.total_available).toFixed(3)}</p> <p class='text-gray-600'>剩余可用额度</p> </div> </div></code>;
} else {
alert('您的账户已被封禁,请登录 OpenAI 进行查看。');
}
}</p>
<p>function checkOpenAICredit() {
var apiKey = document.getElementById('api_key').value;</p>
<pre><code>if (!apiKey.startsWith('sk-')) {
alert('无效的 API 密钥,请检查您的 API 密钥是否正确。');
return;
}
var xhr = new XMLHttpRequest();
showLoader();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var data = JSON.parse(xhr.responseText);
displayResult(data);
hideLoader();
}
};
xhr.open('POST', 'kele.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send('api_key=' + apiKey);
</code></pre>
<p>}</p>
</script>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$apiKey = $_POST['api_key'];
<pre><code>// 在这里添加您的 OpenAI API 请求代码,获取信用额度信息
// 这里只是个示例,具体实现根据您的 API 接口文档来
// $response = file_get_contents('https://api.openai.com/v1/credit', false, stream_context_create([
// 'http' => [
// 'method' => 'GET',
// 'header' => 'Authorization: Bearer ' . $apiKey
// ]
// ]));
// $data = json_decode($response, true);
// 生成模拟数据,仅供示例使用
$data = [
'status' => 'success',
'total_granted' => '10000',
'total_used' => '5000.123',
'total_available' => '4999.877'
];
echo json_encode($data);
</code></pre>
<p>}
?></p>
原文地址: https://www.cveoy.top/t/topic/mR2r 著作权归作者所有。请勿转载和采集!