71 lines
2.1 KiB
PHP
71 lines
2.1 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 = []) {
|
|
$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);
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ($httpCode !== 200) {
|
|
return ['code' => 500, 'msg' => 'Network error or server down'];
|
|
}
|
|
|
|
return json_decode($response, true);
|
|
}
|
|
|
|
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
|
|
]);
|
|
}
|
|
}
|