<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>&lt;div class='grid grid-cols-3 gap-4'&gt; &lt;div class='grid-item bg-gray-100 p-6 rounded-lg text-center'&gt; &lt;p class='text-2xl font-semibold'&gt;${data.total_granted}&lt;/p&gt; &lt;p class='text-gray-600'&gt;总额度&lt;/p&gt; &lt;/div&gt; &lt;div class='grid-item bg-gray-100 p-6 rounded-lg text-center'&gt; &lt;p class='text-2xl font-semibold'&gt;${parseFloat(data.total_used).toFixed(3)}&lt;/p&gt; &lt;p class='text-gray-600'&gt;已使用额度&lt;/p&gt; &lt;/div&gt; &lt;div class='grid-item bg-gray-100 p-6 rounded-lg text-center'&gt; &lt;p class='text-2xl font-semibold'&gt;${parseFloat(data.total_available).toFixed(3)}&lt;/p&gt; &lt;p class='text-gray-600'&gt;剩余可用额度&lt;/p&gt; &lt;/div&gt; &lt;/div&gt;</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 &amp;&amp; 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' =&gt; [
//         'method' =&gt; 'GET',
//         'header' =&gt; 'Authorization: Bearer ' . $apiKey
//     ]
// ]));

// $data = json_decode($response, true);

// 生成模拟数据,仅供示例使用
$data = [
    'status' =&gt; 'success',
    'total_granted' =&gt; '10000',
    'total_used' =&gt; '5000.123',
    'total_available' =&gt; '4999.877'
];

echo json_encode($data);
</code></pre>
<p>}
?&gt;</p>
OpenAI 账户余额查询 - 在线查看剩余信用额度

原文地址: https://www.cveoy.top/t/topic/mR2r 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录