ilgala/laravel-smsfactor

此包已被废弃,不再维护。未建议替代包。

Laravel 5 的 SMSFactor 桥接器 https://www.smsfactor.it/api/

v1.0.0 2017-01-17 13:39 UTC

This package is not auto-updated.

Last update: 2020-01-24 16:47:23 UTC


README

Laravel SMSFactor 由 Filippo Galante 创建并维护,是 PHP SMSFactor API 桥接器,用于 Laravel 5。源代码受到 Graham Campbell 创建的存储库的启发,并使用他的 Laravel Manager 包。您可以随意查看 变更日志发行版许可协议贡献指南。要发送短信,您需要创建一个 SMSFactor 账户,您将可以自由尝试其所有功能。

Latest Version on Packagist Software License StyleCI Build Status Coverage Status Quality Score Total Downloads

安装

需要 PHP 5.5+ 或 HHVM 3.6+。

要获取 Laravel SMSFactor 的最新版本,只需使用 Composer 包要求项目。

$ composer require IlGala/laravel-smsfactor

当然,您也可以手动更新您的 require 块并运行 composer update

{
    "require": {
        "IlGala/laravel-smsfactor": "^1.0"
    }
}

您还需要安装以下依赖项之一,以支持每个驱动程序

  • buzz 连接器需要您的 composer.json 中的 "kriswallsmith/buzz": "^0.15"
  • guzzle 连接器需要您的 composer.json 中的 "guzzle/guzzle": "^3.7"
  • guzzlehttp 连接器需要您的 composer.json 中的 "guzzlehttp/guzzle": "^5.0""guzzlehttp/guzzle": "^6.0"

安装 Laravel SMSFactor 后,您需要注册服务提供者。打开 config/app.php 并将以下内容添加到 providers 键。

  • 'IlGala\SMSFactor\SMSFactorServiceProvider'

您还可以在 config/app.php 文件的 aliases 键中注册 SMSFactor 门面。

  • 'SMSFactor' => 'IlGala\SMSFactor\Facades\SMSFactor'

配置

Laravel SMSFactor 需要连接配置。

要开始,您需要发布所有供应商资产

$ php artisan vendor:publish

这将创建一个可在您的应用中修改以设置配置的 config/smsfactor.php 文件。同时,请确保检查此包中原始配置文件在各个版本之间的更改。

有两个配置选项

默认连接名称

此选项('default')是您指定以下哪个连接作为默认连接的地方,用于所有工作的位置。当然,您可以使用管理类同时使用多个连接。此设置的默认值为 'main'

SMSFactor 连接

此选项('connections')是设置您的应用程序中每个连接的地方。示例配置已包含在内,但您可以根据需要添加任意数量的连接。

用法

SMSFactorManager

这是最感兴趣的类。它绑定到ioc容器上的'smsfactor',可以使用Facades\SMSFactor外观来访问。这个类通过扩展AbstractManager实现了ManagerInterface接口。接口和抽象类都是Graham Campbell Laravel Manager包的一部分,因此您可能想查看该仓库中的文档,了解如何使用管理器类这里

Facades\SMSFactor

这个外观会将静态方法调用动态地传递到ioc容器中的'smsfactor'对象,默认情况下是SMSFactorManager类。

SMSFactorServiceProvider

这个类不包含任何感兴趣的公共方法。这个类应该被添加到config/app.php中的提供者数组中。这个类将设置ioc绑定。

实际示例

在这里,您可以看到这个包是如何简单易用的。默认情况下,默认适配器是main。根据在配置文件中设置的accept属性,响应将自动返回解码后的JSON或SimpleXMLElement。在配置文件中输入您的认证信息后,它将正常工作

use IlGala\SMSFactor\Facades\SMSFactor;
// you can alias this in config/app.php if you like

$total_credits = SMSFactor::credits();
// Check SMS Factor documentation for more information about the results:
/*
 * $total_credits (JSON format):
 * {
 *  "credits": "2420",
 *  "message": "OK"
 * }
 *
 * $total_credits (XML format):
 * <?xml version="1.0" encoding="UTF-8"?>
 * <response>
 *  <credits>2420</credits>
 *  <message>OK</message>
 * </response>
 */

smsfactor管理器将表现得像一个\SMSFactor\SMSFactor类。如果您想调用特定的连接,可以使用connection方法

use IlGala\SMSFactor\Facades\SMSFactor;

// the alternative connection is the other example provided in the default config
SMSFactor::connection('alternative')->credits()->credits;

// let's check how long we have until the limit will reset
SMSFactor::connection('alternative')->credits()->credits;

考虑到这一点,请注意

use IlGala\SMSFactor\Facades\SMSFactor;

// writing this:
SMSFactor::connection('main')->credits();

// is identical to writing this:
SMSFactor::credits();

// and is also identical to writing this:
SMSFactor::connection()->credits();

// this is because the main connection is configured to be the default
SMSFactor::getDefaultConnection(); // this will return main

// we can change the default connection
SMSFactor::setDefaultConnection('alternative'); // the default is now alternative

如果您像我一样更喜欢使用依赖注入而不是外观,则可以轻松地按如下方式注入管理器

use IlGala\SMSFactor\SMSFactorManager;
use Illuminate\Support\Facades\App; // you probably have this aliased already

class SMSSender
{
    protected $smsfactor;

    public function __construct(SMSFactorManager $smsfactor)
    {
        $this->smsfactor = $smsfactor;
    }

    public function sendSms($params, $method, $simulate = false)
    {
        $this->smsfactor->send($params, $method, $simulate = false);
    }
}

App::make('SMSSender')->bar();

有关如何使用幕后调用我们在这里使用的\SMSFactor\SMSFactor类的更多信息,请查看SMSFactor API文档https://github.com/GrahamCampbell/Laravel-Manager#usage中的管理器类。

更多信息

本包中还有其他未在本处记录的类。这是因为它们不是面向公共使用的,而是由本包内部使用的。

安全

如果您在此包中发现安全漏洞,请发送电子邮件至Filippo Galante:filippo.galante@b-ground.com。所有安全漏洞都将得到及时解决。

许可

Laravel SMSFactor采用MIT许可(MIT)