PHP code example of daijulong / laravel-sms
1. Go to this page and download the library: Download daijulong/laravel-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/ */
daijulong / laravel-sms example snippets
$ composer
"daijulong/laravel-sms": "~1.0"
Daijulong\LaravelSms\Providers\SmsServiceProvider::class,
'Sms' => Daijulong\LaravelSms\SmsSender::class,
php artisan vendor:publish --provider="Daijulong\LaravelSms\Providers\SmsServiceProvider"
php artisan sms:create Captcha
namespace App\Sms;
use Daijulong\LaravelSms\Sms;
class Captche extends Sms
{
/**
* Content for agent : Content
*
* return string
*/
protected function agentContent ()
{
return '';
}
/**
* Content for agent : Aliyun
*
* return string
*/
protected function agentAliyun ()
{
return '';
}
}
namespace App\Sms;
use Daijulong\LaravelSms\Sms;
class Captche extends Sms
{
/**
* Content for agent : Content
*
* return string
*/
protected function agentContent ()
{
return '【' . config('sms.sign') . '】验证码:${code},打死也不能告诉别人';
}
/**
* Content for agent : Aliyun
*
* return string
*/
protected function agentAliyun ()
{
return 'SMS_12345678';
}
}
$sms = new App\Sms\Captche();
$result = Sms::send('18012345678', $sms, ['code' => rand(100000,999999)]);
php artisan sms:agent MyAgent
namespace App\Sms\Agents;
use Daijulong\LaravelSms\Agent;
use Daijulong\Sms\Interfaces\Sms;
class MyAgent extends Agent
{
/**
* 代理器名称
*/
protected $agent_name = 'MyAgent';
/**
* 发送短信
*
* @param string $to
* @param Sms $sms
* @param array $params
* @return bool
* @throws SmsException
*/
public function send(string $to, Sms $sms, array $params = []): bool
{
$content = $this->getSmsContent($sms);
// ... 具体发送操作
// ... 记录发送情况
$this->result
->setStatus()
->setMessage('')
->setContent($content)
->setParams($params)
->setReceiptId('')
->setReceiptData('');
// ... return true or false
// return true;
}
}
'agents' => [
// ...
'MyAgent' => [
'key' => '',
],
// ...
],
$sms = new App\Sms\Captche();
$result = Sms::agent('MyAgent', ['Aliyun', 'Content'])->send('18012345678', $sms, ['code' => rand(100000,999999)]);
$sms = new App\Sms\Captche();
$result = Sms::onlyAgent('MyAgent')->send('18012345678', $sms, ['code' => rand(100000,999999)]);
php artisan sms:table
php artisan migrate
默认代理器(default_agent) > 备用代理器(spare_agents)
$this->config['key']