'https://sms-api.luosimao.com/v1/send.json',
'send_batch' => 'https://sms-api.luosimao.com/v1/send_batch.json',
'status' => 'https://sms-api.luosimao.com/v1/status.json',
];
private $_api_url = [
'send' => 'https://sms-api.luosimao.com/v1/send.json',
'send_batch' => 'https://sms-api.luosimao.com/v1/send_batch.json',
'status' => 'https://sms-api.luosimao.com/v1/status.json',
];
public function __construct(array $param = [])
{
if (!isset($param['api_key'])) die("api key error.");
$this->_api_key = $param['api_key'];
if (isset($param['use_ssl'])) $this->_use_ssl = $param['use_ssl'];
}
// 單發
public function send($mobile, $message)
{
$url = $this->_use_ssl ? $this->_ssl_api_url['send'] : $this->_api_url['send'];
return $this->http_post($url, ['mobile'=>$mobile, 'message'=>$message]);
}
// 批量
public function send_batch($mobile_list, $message, $time = '')
{
$url = $this->_use_ssl ? $this->_ssl_api_url['send_batch'] : $this->_api_url['send_batch'];
$mobiles = is_array($mobile_list) ? implode(',', $mobile_list) : $mobile_list;
return $this->http_post($url, ['mobile_list'=>$mobiles, 'message'=>$message, 'time'=>$time]);
}
// 余額
public function get_deposit()
{
$url = $this->_use_ssl ? $this->_ssl_api_url['status'] : $this->_api_url['status'];
return $this->http_get($url);
}
// 接收狀態報告 / 上行回復 (回調入口)
public function recv($type = 'status', array $param = [])
{
if ($type === 'status' && isset($param['batch_id'], $param['mobile'], $param['status'])) {
// 記錄發送狀態
} elseif ($type === 'incoming' && isset($param['mobile'], $param['message'])) {
// 記錄用戶回復
}
}
private function http_post($url, array $param, $timeout = 5)
{
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
CURLOPT_CONNECTTIMEOUT => $timeout,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => 'api:key-' . $this->_api_key,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $param,
]);
if (parse_url($url, PHP_URL_SCHEME) === 'https') {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
$res = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) { $this->_last_error[] = $err; return false; }
return json_decode($res, true);
}
private function http_get($url, $timeout = 5)
{
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
CURLOPT_CONNECTTIMEOUT => $timeout,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => 'api:key-' . $this->_api_key,
]);
if (parse_url($url, PHP_URL_SCHEME) === 'https') {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
$res = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) { $this->_last_error[] = $err; return false; }
return json_decode($res, true);
}
public function last_error() { return $this->_last_error; }
}
?>
// 配置
require 'sms.php';
$sms = new Sms(['api_key' => 'your_key_from_luosimao', 'use_ssl' => true]);
// 1. 單發驗證碼
$res = $sms->send('13800000000', '您的驗證碼:123456【簽名】');
if ($res && $res['error'] == 0) echo '發送成功'; else var_dump($res);
// 2. 批量通知
$res = $sms->send_batch(['13800000000','13900000000'], '系統維護通知【簽名】');
// 3. 查余額
$res = $sms->get_deposit();
if ($res && $res['error'] == 0) echo "余額: {$res['deposit']} 條";
| 點 | 說明 |
|---|---|
| 認證方式 | HTTP Basic Auth,用戶名固定 api,密碼 key-{$api_key}。 |
| HTTPS 選項 | use_ssl=true 切換到 HTTPS 端點,關閉證書驗證 (簡化部署),生產建議開啟驗證並配置 CA。 |
| 批量發送 | 手機號逗號分隔,單次上限視運營商限制 (通常 1000-2000 號/次)。 |
| 定時發送 | send_batch 的 time 參數支持 Unix 時間戳或 YYYY-MM-DD HH:MM:SS。 |
| 回調處理 | recv() 需在公開可訪問的 URL 接收 POST,驗證簽名防偽造。 |
| 錯誤收集 | _last_error 記錄 cURL 錯誤,便於排查網絡/認證問題。 |
sign = md5(api_key + mobile + ...))。本 Sms 類封裝了螺絲帽 (Luosimao) 短信 API 的核心能力:單發、批量、余額、回調。代碼結構清晰,易於擴展至其他服務商 (阿里云、騰訊云、網易云信等)。實際項目中請務必加上「頻控、審核、異步、多通道、日誌」五大工程化保障。