bigbharatjain/laravel-clickatell

此包使您能够使用Laravel 5轻松通过clickatell.com发送短信。

1.0.1 2021-05-02 20:21 UTC

This package is auto-updated.

Last update: 2024-09-29 05:09:13 UTC


README

适用于Laravel 5.0及以上版本。此包允许您与新的Clickatell网站集成。

介绍

在您的Laravel应用程序中集成Clickatell并使用此包发送短信。

入门

要开始,请将以下包添加到您的composer.json文件中。

composer require bigbharatjain/laravel-clickatell

配置

当Composer成功安装Clickatell库后,请将Clickatell\ClickatellServiceProvider注册到您的config/app.php配置文件中。

'providers' => [
    // Other service providers...
    Clickatell\ClickatellServiceProvider::class,
],

此外,将Clickatell外观添加到您的app配置文件中的aliases数组中

'aliases' => [
    // Other aliases
    'Clickatell' => Clickatell\ClickatellFacade::class,
],

只需再走一步....

它将在配置文件夹中发布一个clickatell.php。在此处设置Clickatell api_key或创建环境变量CLICKATELL_API_KEY

使用方法

新的API仅支持sendMessage调用以及通过RESTful接口发送和接收消息的webhooks。

namespace App\Http\Controllers;

use Clickatell\Rest;
use Clickatell\ClickatellException;

class SendSmsController extends Controller
{
$clickatell = new \Clickatell\Rest();

// Full list of support parameters can be found at https://www.clickatell.com/developers/api-documentation/rest-api-request-parameters/
try {
    $result = $clickatell->sendMessage(['to' => ['27111111111'], 'content' => 'Message Content']);

    foreach ($result['messages'] as $message) {
        var_dump($message);

        /*
        [
            'apiMsgId'  => null|string,
            'accepted'  => boolean,
            'to'        => string,
            'error'     => null|string
        ]
        */
    }

} catch (ClickatellException $e) {
    // Any API call error will be thrown and should be handled appropriately.
    // The API does not return error codes, so it's best to rely on error descriptions.
    var_dump($e->getMessage());
}

状态/回复回调

在开发门户内配置您的webhooks/callbacks后,您可以使用静态回调方法来监听来自Clickatell的web请求。这些回调将从请求体中提取支持的字段。

use Clickatell\Rest;
use Clickatell\ClickatellException;

// Outgoing traffic callbacks (MT callbacks)
Rest::parseStatusCallback(function ($result) {
    var_dump($result);
    // This will execute if the request to the web page contains all the values
    // specified by Clickatell. Requests that omit these values will be ignored.
});

// Incoming traffic callbacks (MO/Two Way callbacks)
Rest::parseReplyCallback(function ($result) {
    var_dump($result);
    // This will execute if the request to the web page contains all the values
    // specified by Clickatell. Requests that omit these values will be ignored.
});

致谢

Clickatell平台PHP库

问题/贡献

发现错误或缺少功能?请在此记录,我将查看。