首页 > 建站‧营销

查询chatGPT API密钥余额的PHP代码


<?php

// OpenAI API密钥
$api_key = 'YOUR_API_KEY';

// 查询余额的接口地址
$url = 'https://api.openai.com/v1/balance';

// 设置请求头部信息
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
);

// 发送请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

// 解析响应数据
$data = json_decode($response, true);

或者是:

<?php
 
require_once('MkEncrypt.php');
 
MkEncrypt('123456');     //这个就是密码

?>

<!DOCTYPE html>
<html>
<head>
<title>API余额查询</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
}
h1 {
text-align: center;
margin-top: 50px;
}
form {
margin: 50px auto;
width: 50%;
padding: 20px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0px 0px 10px rgba(0,0,0,0.2);
}
label {
display: block;
margin-bottom: 10px;
font-weight: bold;
}
input[type="text"] {
width: 100%;
padding: 10px;
border-radius: 5px;
border: none;
margin-bottom: 20px;
box-shadow: 0px 0px 5px rgba(0,0,0,0.1);
}
input[type="submit"] {
background-color: #4CAF50;
color: #fff;
border: none;
padding: 10px;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s ease;
}
input[type="submit"]:hover {
background-color: #3e8e41;
}
.result {
margin: 50px auto;
width: 50%;
padding: 20px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0px 0px 10px rgba(0,0,0,0.2);
display: none;
}
.result h2 {
margin-top: 0px;
}
.result p {
margin-bottom: 10px;
}
</style>
</head>
<body>
<h1>API余额查询</h1>
<form id="balance-form">
<label for="api-key">API密钥:</label>
<input type="text" id="api-key" name="api_key" required>
<input type="submit" value="查询余额">
</form>
<section id="wjc" style="border:solid 1px #000;padding:15px;margin-top:2px;margin: 50px auto;width: 50%;"> 
 <section style="background-color:#f6f6f6;padding:15px;"> 
 <section style="font-size:14px;letter-spacing:2px;line-height:1.75;" class=""> 
<P>可能有以下几个原因导致查询余额显示失败:</P>
<P>API密钥无效或过期:请确保您使用的API密钥是有效的,并且没有过期。您可以在OpenAI控制台中生成新的API密钥。</P>
<P>访问限制:如果您的API密钥没有足够的权限来访问余额信息,则查询余额将失败。请确保您的API密钥具有足够的权限。</P>
<P>网络问题:如果您的网络连接不稳定或存在其他问题,则可能无法成功查询余额。请检查您的网络连接并重试。</P>
<P>服务器问题:如果OpenAI服务器出现故障或维护,则可能无法成功查询余额。请稍后再试。</P>

</section> 
 </section> 
 </section> 
<div class="result">
<h2>余额信息:</h2>
<p><strong>剩余请求次数:</strong><span id="remaining"></span></p>
<p><strong>重置时间:</strong><span id="reset"></span></p>
</div>
<script>
$(document).ready(function() {
$('#balance-form').submit(function(e) {
e.preventDefault();
var api_key = $('#api-key').val();
var url = 'https://openai.1rmb.tk/dashboard/billing/credit_grants';//https://api.openai.com/v1/balance;
$.ajax({
type: 'GET',
url: url,
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + api_key);
},
success: function(data) {
$('.result').show();
$('#remaining').text(data.data[0].attributes.remaining_requests);
$('#reset').text(data.data[0].attributes.reset_at);
},
error: function(xhr, status, error) {
alert('查询失败:' + error);
}
});
});
});
</script>
</body>
</html>

本文链接:https://www.zhanque.net/cms/3009.html