38320-vm/api/LocalLubanApi.php
Flatlogic Bot f09a03c89c 最新
2026-02-10 11:39:11 +00:00

86 lines
2.6 KiB
PHP

<?php
require_once __DIR__ . '/../db/config.php';
class LubanSMS {
private $apikey;
private $baseUrl = 'https://lubansms.com/v2/api/';
public function __construct($apikey = null) {
if ($apikey) {
$this->apikey = $apikey;
} else {
$pdo = db();
$stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'lubansms_apikey'");
$stmt->execute();
$this->apikey = $stmt->fetchColumn();
}
}
private function request($endpoint, $params = []) {
if (!$this->apikey) {
return ['code' => 500, 'msg' => 'API Key not configured'];
}
$params['apikey'] = $this->apikey;
$url = $this->baseUrl . $endpoint . '?' . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
if ($response === false) {
$error = curl_error($ch);
curl_close($ch);
return ['code' => 500, 'msg' => 'CURL Error: ' . $error];
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
return ['code' => 500, 'msg' => 'API Server returned HTTP ' . $httpCode];
}
$decoded = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return ['code' => 500, 'msg' => 'Invalid JSON from API'];
}
return $decoded;
}
public function getBalance() {
return $this->request('getBalance');
}
public function getCountries() {
return $this->request('countries');
}
public function getServices($countryName = '', $serviceName = '', $page = 1) {
$params = [
'page' => $page,
'language' => 'zh'
];
if ($countryName) $params['country'] = $countryName;
if ($serviceName) $params['service'] = $serviceName;
return $this->request('List', $params);
}
public function getNumber($service_id) {
return $this->request('getNumber', ['service_id' => $service_id]);
}
public function getSms($request_id) {
return $this->request('getSms', ['request_id' => $request_id]);
}
public function setStatus($request_id, $status = 'reject') {
return $this->request('setStatus', [
'request_id' => $request_id,
'status' => $status
]);
}
}