PHP code example of mikecai / php-sms
1. Go to this page and download the library: Download mikecai/php-sms library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mikecai / php-sms example snippets
composer
composer
//example:
Sms::config([
'Every8d' => [
'mdm_number' => 'your_mdm_number',
'username' => 'your_username',
'password' => 'your_password',
],
'AptgXsms' =>[
'mdn_number' => 'your_mdm_number', // 手機門號
'username' => 'your_username', //帳號
'password' => 'your_password', //密碼
]
]);
//example:
Sms::scheme([
//被使用概率为2/3
'Every8d' => '20',
//被使用概率為1/3,且为備用代理器
'AptgXsms' => '10 backup',
//僅為備用代理器
'SmsBao' => '0 backup',
]);
use mikecai\PhpSms\Sms;
// 接收人手機號
$to = '0987654321';
// 簡訊内容
$content = '【簽名】这是簡訊内容...';
// 使用内容方式發送(如:Every8d)
$result = Sms::make()->to($to)->content($content)->send();
//$result ,內容會返回 batchId
$batchId = "a7801510-c427-4665-bd8e-ce10be816a1b";
//查詢簡訊發送狀態
$sms_status = Sms::status()->query($batchId)->send();
//服务提供器
'providers' => [
...
mikecai\PhpSms\PhpSmsServiceProvider::class,
]
//别名
'aliases' => [
...
'PhpSms' => mikecai\PhpSms\Facades\Sms::class,
]
php artisan vendor:publish
PhpSms::make()->to($to)->content($content)->send();
Sms::scheme([
'Every8d' => '80 backup'
'AptgXsms' => '100 backup'
]);
//或
Sms::scheme('Every8d', '80 backup');
Sms::scheme('AptgXsms', '100 backup');
//获取所有的调度方案:
$scheme = Sms::scheme();
//获取指定代理器的调度方案:
$scheme['SmsBao'] = Sms::scheme('SmsBao');
Sms::config([
'Every8d' => [
'username' => ...,
'password' => ...,
]
]);
//或
Sms::config('Every8d', [
'username' => ...,
'password' => ...,
]);
//獲取所有的配置:
$config = Sms::config();
//獲取指定代理器的配置:
$config['Every8d'] = Sms::config('Every8d');
Sms::beforeSend(function($task, $index, $handlers, $prevReturn){
//獲取簡訊數據
$smsData = $task->data;
...
//如果返回false會終止發送任務
return true;
});
Sms::beforeAgentSend(function($task, $driver, $index, $handlers, $prevReturn){
//簡訊資料:
$smsData = $task->data;
//當前使用的代理器名稱:
$agentName = $driver->name;
//如果返回false會停止使用當前代理器
return true;
});
Sms::afterAgentSend(function($task, $agentResult, $index, $handlers, $prevReturn){
//$result为代理器的發送结果数据
$agentName = $agentResult['driver'];
...
});
Sms::afterSend(function($task, $taskResult, $index, $handlers, $prevReturn){
//$result為發送後獲得的結果數組
$success = $taskResult['success'];
...
});
//自动启用队列
Sms::queue(function($sms, $data){
//define how to push to queue.
...
});
//第一个参数为true,启用队列
Sms::queue(true, function($sms, $data){
//define how to push to queue.
...
});
//第一个参数为false,暂时关闭队列
Sms::queue(false, function($sms, $data){
//define how to push to queue.
...
});
Sms::queue(true);//开启队列
Sms::queue(false);//关闭队列
$enable = Sms::queue();
//为true,表示当前启用了队列。
//为false,表示当前关闭了队列。
$sms = Sms::make();
//创建实例的同时设置簡訊内容:
$sms = Sms::make('【签名】这是簡訊内容...');
//创建实例的同时设置簡訊模版:
$sms = Sms::make('YunTongXun', 'your_temp_id');
//或
$sms = Sms::make([
'YunTongXun' => 'your_temp_id',
'SubMail' => 'your_temp_id',
...
]);
//從發送簡訊返回的ID
$batchId = "a7801510-c427-4665-bd8e-ce10be816a1b";
//查詢簡訊發送狀態
$sms_status = Sms::status()->query($batchId)->send();
$sms = Sms::voice();
//创建实例的同时设置驗證碼
$sms = Sms::voice($code);
$sms->to('0987654321');
//单个数据
$sms->data('code', $code);
//同时设置多个数据
$sms->data([
'code' => $code,
'minutes' => $minutes
]);
$sms->content('【签名】这是簡訊内容...');
$sms->params('Agent1', [
'callbackUrl' => ...,
'userData' => ...,
]);
//或
$sms->params([
'Agent1' => [
'callbackUrl' => ...,
'userData' => ...,
],
'Agent2' => [
...
],
]);
[
'type' => ...,
'to' => ...,
'data' => [...], // template data
'content' => ...,
'params' => [...],
]
$sms->agent('Every8d');
//会遵循是否使用队列
$result = $sms->send();
//忽略是否使用队列
$result = $sms->send(true);
//example:
'Foo' => [
'key' => 'your api key',
...
]
Sms::scheme('agentName', [
'10 backup',
'agentClass' => 'My\Namespace\MyAgentClass'
]);
Sms::scheme([
'agentName' => [
'20 backup',
'sendContentSms' => function($agent, $to, $content){
// 獲取配置(如果設置了的話):
$key = $agent->key;
...
// 可使用的内置方法:
$agent->curlGet($url, $params); //get
$agent->curlPost($url, $params); //post
...
// 更新發送結果:
$agent->result(Agent::SUCCESS, true);
$agent->result(Agent::INFO, 'some info');
$agent->result(Agent::CODE, 'your code');
},
'sendVoiceCode' => function($agent, $to, $code){
// 發送語音驗證碼,同上
}
]
]);