functiON sendCurlRequest($url, $headers, $data) {
$ch= curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_POST, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$apiKey = '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';
$headers = array( 'Authorization: Bearer ' . $apiKey, 'Content-Type: application/json');
$url = "https://api.openai.com/v1/dashboard/billing/usage?start_date=".date('Y-m-01')."&end_date=".date('Y-m-t');
$response = sendCurlRequest($url, $headers, $data);
$max_usage =100;//通知触发阈值
if(strstr($response,'error')){
echo "invalid_api_key";}else{
$cost_data=json_decode($response);
$total_usage=round($cost_data->total_usage/100,2);
}
if( $total_usage>$max_usage){
$url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693axxx6-7aoc-4bc4-97a0-0ec2sifa5aaa';
$headers = array('Content-Type: application/json');
$data = array("msgtype" => "text", "text" => array("content" => "余额不足","mentioned_list" => array("mjj","@all")));
$response = sendCurlRequest($url, $headers, $data);
echo "余额不足";
}
评论专区