Easy SMS增加短信平台

Easy SMS是一个非常好用的短信发送组件,大多数厂家短信都可以使用,无奈短信平台太多,偶尔会遇到Easy SMS没有的平台,那就只能自己再造个轮子。

1、进入/vendor/overtrue/easy-sms/src/Gateways目录下,新建文件 : MeilianGateway.php 

2、代码如下

<?php
/**
 * Created by PhpStorm.
 * User: lsq
 * Date: 2020/12/09
 * Time: 15:57
 */
namespace Overtrue\EasySms\Gateways;
use Overtrue\EasySms\Contracts\MessageInterface;
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
use Overtrue\EasySms\Exceptions\GatewayErrorException;
use Overtrue\EasySms\Gateways\Gateway;
use Overtrue\EasySms\Support\Config;
use Overtrue\EasySms\Traits\HasHttpRequest;
class MeilianGateway extends Gateway
{
    use HasHttpRequest;
    const ENDPOINT_HOST = 'http://m.5c.com.cn/api/send/index.php?';
    protected $encode;  //页面编码和短信内容编码为GBK。重要说明:如提交短信后收到乱码,请将GBK改为UTF-8测试。如本程序页面为编码格式为:ASCII/GB2312/GBK则该处为GBK。如本页面编码为UTF-8或需要支持繁体,阿拉伯文等Unicode,请将此处写为:UTF-8
    protected $username;  //用户名
    protected $password_md5;  //32位MD5密码加密,不区分大小写
    protected $apikey;  //apikey秘钥(请登录 http://m.5c.com.cn 短信平台-->账号管理-->我的信息 中复制apikey)

    //发送接口
    public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
    {
        //发送链接(用户名,密码,apikey,手机号,内容)
        $data = [
            'username' => $config->get('username'),
            'password_md5' => $config->get('password_md5'),
            'apikey' => $config->get('apikey'),
            'mobile' => $to->getNumber(),
            'content' => $message->getContent(),
            'encode' => $config->get('encode'),
        ];
        $result = $this->post(self::ENDPOINT_HOST, $data);
        if (strpos($result, "success") === false) {
            throw new GatewayErrorException($result, '400', [$result]);
        }
        return $result;
    }

    function post($url, $post_fields = array())
    {
        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);//用PHP取回的URL地址(值将被作为字符串)
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//使用curl_setopt获取页面内容或提交数据,有时候希望返回的内容作为变量存储,而不是直接输出,这时候希望返回的内容作为变量
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);//30秒超时限制
            curl_setopt($ch, CURLOPT_HEADER, 1);//将文件头输出直接可见。
            curl_setopt($ch, CURLOPT_POST, 1);//设置这个选项为一个零非值,这个post是普通的application/x-www-from-urlencoded类型,多数被HTTP表调用。
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);//post操作的所有数据的字符串。
            $data = curl_exec($ch);//抓取URL并把他传递给浏览器
            curl_close($ch);//释放资源
            return $data; //然后在这里返回数组。
        } catch (\Exception $e) {
            dump($e->getMessage());
            die;
        }
    }
}

3、轮子造好了,开始使用

$config = [
    // HTTP 请求的超时时间(秒)
    'timeout' => 5.0,
    // 默认发送配置
    'default' => [
        // 网关调用策略,默认:顺序调用
        'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
        // 默认可用的发送网关
        'gateways' => [
            'meilian', 'aliyun',
        ],
    ],
    // 可用的网关配置
    'gateways' => [
        'errorlog' => [
            'file' => '/tmp/easy-sms.log',
        ],
        'aliyun' => [
            'api_key' => '824f0ff2f71cab52936axxxxxxxxxx',
        ],
        'meilian' => [
            'username' => '',
            'password_md5' => '',
            'encode' => '',
            'apikey' => ''
        ],
    ],
];

$sms = new EasySms($config);

//短信发送
$sms->send($phone, [
    'content' => '【美联】您的验证码是' . $sms_codes . '。如非本人操作,请忽略本短信',
]);

 

4、大功告成,更多使用方法参考:https://github.com/overtrue/easy-sms

 

posted @ 2020-12-08 22:24  北漂生活  阅读(435)  评论(0编辑  收藏  举报