jaby/sms

适用于任何公司的伊朗短信发送包

1.0.19 2023-05-23 10:42 UTC

README

alt text

Laravel Sms Services

Software License Latest Version on Packagist Total Downloads on Packagist Scrutinizer Code Quality

这是一个用于集成短信服务的Laravel包。此包支持 Laravel 5.8+

如果你喜欢这个包,请捐赠我 <3

对于PHP集成,你可以使用jaby/sms包。

这个包支持多个驱动程序,如果你在下面的当前驱动程序列表中找不到它们,你可以创建自定义驱动程序。

可用驱动程序列表

通过创建 pull requests 来帮助我添加以下服务

  • mellipayamak.com
  • farapayamak.ir
  • ...

所有与ippanel兼容的服务都可以使用默认服务 farazsms

如果列表中没有,你可以创建自己的自定义驱动程序,请阅读 创建自定义驱动程序 部分。

安装

通过Composer

$ composer require jaby/sms

配置

如果你正在使用 Laravel 5.5 或更高版本,则不需要添加提供者和别名。 (跳到 b)

a. 在你的 config/app.php 文件中添加这两行。

// In your providers array.
'providers' => [
    ...
    Jaby\Sms\SmsServiceProvider::class,
],

// In your aliases array.
'aliases' => [
    ...
    'Sms' => Jaby\Sms\Sms::class,
],

b. 然后,运行 php artisan vendor:publish 在你的配置目录中发布 config/sms.php 文件。

在配置文件中,你可以设置所有发送者使用的默认驱动程序。但您也可以在运行时更改驱动程序。

选择您想要在应用程序中使用的服务。然后将其作为默认驱动程序,这样您就不必在每处都指定它。但是,您也可以在项目中使用多个服务。

// Eg. if you want to use zarinpal.
'default' => 'farazsms',

然后在驱动程序数组中填写该服务的凭证。

'drivers' => [
    'farazsms' => [
        'username'    => 'username',
        'password'    => 'password',
        'urlPattern'  => 'https://ippanel.com/patterns/pattern',
        'urlNormal'   => 'https://ippanel.com/services.jspd',
        'from'        => '+983000505',
    ],
    ...
]

如何使用

可用方法

  • driver: 设置驱动程序
  • text: 设置不带模式的要发送的消息
  • pattern: 设置您的模式代码
  • data: 设置数据模式数组
  • to: 设置接收者号码数组
  • from: 设置发送者号码
  • send: 发送您的短信

示例

Sms::text('Hello')->to(['numbers'])->send();

Sms::driver('your driver')->text('Hello')->to(['numbers'])->send();

Sms::pattern('your pattent code')->data([
    'name' => $name ,
    'code' => $code
])->to(['numbers'])->send();

Sms::...->from('sender number')-> ...->send();

创建自定义驱动程序

首先,您必须在驱动程序数组中添加您驱动程序的名字,并且您还可以指定您想要的任何配置参数。

'drivers' => [
    'farazsms' => [...],
    'my_driver' => [
        ... // Your Config Params here.
    ]
]

现在,您必须创建一个Driver Map类,该类将用于发送短信。

例如:您创建了一个类: App\Packages\Sms\MyDriver

namespace App\Packages\Sms;

use Jaby\Sms\SmsInterface;

class MyDriver extends SmsInterface
{
    protected $drive = 'farazsms';

    protected $method;

    protected $username;

    protected $password;

    protected $from;

    protected $pattern_code;

    protected $to;

    protected $input_data;

    protected $url;

    protected $numbers;

    protected $data;

    protected $text;

    /**
     * farazsms constructor.
     */
    public function __construct()
    {
        $this->username = config('sms.drivers.'.$this->drive.'.username');
        $this->password = config('sms.drivers.'.$this->drive.'.password');
        $this->from     = config('sms.drivers.'.$this->drive.'.from');
        $this->url      = config('sms.drivers.'.$this->drive.'.urlPattern');
    }

    /**
     * @return bool|mixed|string
     */
    public function send()
    {
        if ($this->method == 'pattern')
            $res = $this->sendPattern();
        else
            $res = $this->message($this->text);
        return $res;
    }

    /**
     * @param $text
     * @return $this|mixed
     */
    public function text($text)
    {
        $this->text = $text;

        return $this;
    }

    /**
     * @param null $pattern_code
     * @return $this|mixed
     */
    public function pattern($pattern_code = null)
    {
        $this->method = 'pattern';
        if ($pattern_code)
            $this->pattern_code = $pattern_code;
        return $this;
    }

    /**
     * @param array $data
     * @return $this|mixed
     */
    public function data(array $data)
    {
        $this->data = $data;

        return $this;
    }

    /**
     * @param $from
     * @return $this|mixed
     */
    public function from($from)
    {
        $this->from = $from;

        return $this;
    }

    /**
     * @param array $numbers
     * @return $this|mixed
     */
    public function to(array $numbers)
    {
        $this->numbers = $numbers;

        return $this;
    }

    /**
     * @return bool|mixed|string
     */
    public function sendPattern()
    {
        $numbers       = $this->numbers;
        $pattern_code  = $this->pattern_code;
        $username      = $this->username;
        $password      = $this->password;
        $from          = $this->from;
        $to            = $numbers;
        $input_data    = $this->data;
        $url = $this->url."?username=" . $username . "&password=" . urlencode($password) . "&from=$from&to=" . json_encode($to) . "&input_data=" . urlencode(json_encode($input_data)) . "&pattern_code=$pattern_code";
        $handler = curl_init($url);
        curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($handler, CURLOPT_POSTFIELDS, $input_data);
        curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($handler);
        return $response;
    }

    /**
     * @param $text
     * @return mixed
     */
    public function message($text)
    {

        $this->url   = config('sms.drivers.'.$this->drive.'.urlNormal');

        $rcpt_nm = $this->numbers;
        $param = array
        (
            'uname'=> $this->username ,
            'pass'=> $this->password,
            'from'=>$this->from,
            'message'=>$text,
            'to'=>json_encode($rcpt_nm),
            'op'=>'send'
        );

        $handler = curl_init($this->url);
        curl_setopt($handler, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($handler, CURLOPT_POSTFIELDS, $param);
        curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
        $response2 = curl_exec($handler);

        $response2 = json_decode($response2);
        $res_code = $response2[0];
        $res_data = $response2[1];

        return $res_data;
    }
}

一旦创建了该类,您必须在该sms.php配置文件的map部分中指定它。

'map' => [
    ...
    'my_driver' => App\Packages\Sms\MyDriver::class,
]

注意: 您必须确保map数组的关键字与drivers数组的关键字相同。

安全

如果您发现任何与安全相关的问题,请通过电子邮件jaber_sabzali@yahoo.com,而不是使用问题跟踪器。

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件