jackmartin/bsg-php

BSG.WORLD API 库

1.0.0 2019-10-09 12:23 UTC

This package is auto-updated.

Last update: 2024-09-10 00:32:08 UTC


README

此仓库包含 BSG REST API 的开源 Php 客户端。

需求

  • 注册 免费账户
  • 获取 API 密钥

安装

下载并使用 "src" 目录中的文件。不需要安装。Composer 仅用于 phpunit 测试。

BSG 类

Bsg 类提供对 hlr/sms/viber 客户端的访问

参数

apiKey (string) 来自 bsg.world 的 API 密钥。必需。

sender (string) sms 的发送者名称

viberSender (string) viber 的发送者名称(alpha 名称)

tariff (int) 应用于此会话的资费

api source (string) 您的应用程序名称(非必需)

$apiKey = 'test_8zWaPwOaHDPRFmgIldhl'
$bsg = new BSG($apiKey, 'originator', 'aplhaname', 3, 'Bitrix-24')

API 函数 "balance"

BSG 提供了一个 API 来获取您账户的余额信息。

$apikey = 'test_8zWaPwOaHDPRFmgIldhl';
$api = new BSG($apikey);
$client = $api->getSmsClient(); //get one of the clients
$balance = $client->getBalance();

简单响应

$balance = array(5) {
             ["error"]=>
             int(0)
             ["errorDescription"]=>
             string(9) "No errors"
             ["amount"]=>
             string(10) "-4.0999333"
             ["currency"]=>
             string(3) "EUR"
             ["limit"]=>
             string(1) "7"
           }

API 函数 "sendHLR"

号码查找可以帮助您保持您的手机号码数据库更新。

手机用户经常更改号码,漫游并更改运营商,同时保留原始手机号码。了解哪些手机号码正在使用,或哪些网络客户目前正在使用,可以大大提高许多类型企业的准确性和成本效益。使用号码查找,您可以确定

  • 哪些号码当前活跃
  • 手机号码是否在漫游
  • 手机号码是否已转网

参数

msisdn (int) 电话号码。必需。

reference (string) 客户端参考。必需。

tariff (int) 价格表的资费代码。如果为空,使用 Bsg 类的值。如果没有设置,则使用 bsg 画室的默认值

HLR 请求

$hlr = $bsg->getHLRClient();
$responce = $hlr->sendHLR('+380501111111', (string)time());

示例响应

   $response = array(3) {
                ["result"]=>
                array(1) {
                  [0]=>
                  array(9) {
                    ["error"]=>
                    int(0)
                    ["errorDescription"]=>
                    string(9) "No errors"
                    ["msisdn"]=>
                    string(12) "380501111111"
                    ["reference"]=>
                    string(10) "1488536778"
                    ["tariff_code"]=>
                    string(1) "0"
                    ["callback_url"]=>
                    string(0) ""
                    ["price"]=>
                    float(0.0025)
                    ["currency"]=>
                    string(3) "EUR"
                    ["id"]=>
                    string(6) "174568"
                  }
                }
                ["total_price"]=>
                float(0.0025)
                ["currency"]=>
                string(3) "EUR"
              }

多个 HLR 请求。API 函数 sendHLRS

一次发送多个 hlr 查询。
示例

$response = $hlr->sendHLRS(
      [ 
        [
          "msisdn"=>"380501111111", //required
          "reference"=>"extid1",  //required
          "tariff"=>"0",
        ],
        [
          "msisdn"=>"380501111112",
          "reference"=>"extid2",
          "tariff"=>"0",
        ]
      ]
)

示例响应

$response = {array(3) {
               ["result"]=>
               array(2) {
                 [0]=>
                 array(8) {
                   ["error"]=>
                   int(0)
                   ["msisdn"]=>
                   string(12) "380501111111"
                   ["reference"]=>
                   string(14) "extid1"
                   ["tariff_code"]=>
                   string(1) "0"
                   ["callback_url"]=>
                   string(0) ""
                   ["price"]=>
                   float(0.0025)
                   ["currency"]=>
                   string(3) "EUR"
                   ["id"]=>
                   string(6) "174573"
                 }
                 [1]=>
                 array(8) {
                   ["error"]=>
                   int(0)
                   ["msisdn"]=>
                   string(12) "380501111112"
                   ["reference"]=>
                   string(14) "extid2"
                   ["tariff_code"]=>
                   string(1) "0"
                   ["callback_url"]=>
                   string(0) ""
                   ["price"]=>
                   float(0.0025)
                   ["currency"]=>
                   string(3) "EUR"
                   ["id"]=>
                   string(6) "174574"
                 }
               }
               ["total_price"]=>
               float(0.005)
               ["currency"]=>
               string(3) "EUR"
             }

API 函数 "getStatusByReference"

检索现有 HLR 的信息。您只需要提供创建时设置的唯一消息参考。

$response = $hlr->getStatusByReference('ref1);

示例响应

$response = array(14) {
              ["error"]=>
              int(0)
              ["errorDescription"]=>
              string(9) "No errors"
              ["name_ru"]=>
              string(14) "Украина"
              ["name_en"]=>
              string(7) "Ukraine"
              ["brand"]=>
              string(3) "MTS"
              ["name"]=>
              string(11) "MTS Ukraine"
              ["msisdn"]=>
              string(12) "380501111111"
              ["id"]=>
              string(12) "380501111111"
              ["reference"]=>
              string(12) "su1488546296"
              ["network"]=>
              string(5) "25501"
              ["status"]=>
              string(6) "failed"
              ["details"]=>
              NULL
              ["createdDatetime"]=>
              string(25) "2017-03-03T13:04:56+00:00"
              ["statusDatetime"]=>
              string(25) "2017-03-03T13:04:56+00:00"
            }

API 函数 "getStatusById"

检索现有 HLR 的信息。您只需要提供创建或接收时返回的唯一消息 id。
示例

$response = $hlr->getStatusById('1);

示例响应

$response = array(14) {
              ["error"]=>
              int(0)
              ["errorDescription"]=>
              string(9) "No errors"
              ["name_ru"]=>
              string(14) "Украина"
              ["name_en"]=>
              string(7) "Ukraine"
              ["brand"]=>
              string(3) "MTS"
              ["name"]=>
              string(11) "MTS Ukraine"
              ["msisdn"]=>
              string(12) "380501111111"
              ["id"]=>
              string(12) "380501111111"
              ["reference"]=>
              string(12) "su1488546296"
              ["network"]=>
              string(5) "25501"
              ["status"]=>
              string(6) "failed"
              ["details"]=>
              NULL
              ["createdDatetime"]=>
              string(25) "2017-03-03T13:04:56+00:00"
              ["statusDatetime"]=>
              string(25) "2017-03-03T13:04:56+00:00"
            }

SMS 消息

BSG 提供了一个 API 来向世界任何国家发送 SMS 消息。

BSG 通过一个唯一的随机 ID 识别。您可以使用此 ID 通过提供的端点始终检查消息的状态。

API 函数 sendSms

创建一个新的消息对象。BSG 在每次请求中返回创建的消息对象。每个请求最多可输入 50 部电话。

参数

msisdn (string) 电话号码。必需

body (string) SMS 消息的内容。必需

reference (string) 客户端参考。必需

validity (int) 消息有效的秒数。

tariff (int) 价格表的资费代码。

originator (string) 消息的发送者。这可以是带有国家代码的电话号码或字母数字字符串。如果是字母数字字符串,最大长度为 11 个字符。如果已设置 BSG,则使用该值。在其他所有情况下 - 必需

单个短信

$smsClient = $BSG->getSmsClient();
$smsClient->sendSms(
    '380501111111', 
    'test', 
    'successSend' . (string)time()
);

多个短信

$smsclient->$answer = $this->smsClient->sendSmsMulti([
                            ['msisdn' => 380501111111, 'body' =>'test', 'reference' => 'successSendM' . (string)time()],
                            ['msisdn' => 380501111112, 'body' =>'tes2', 'reference' => 'successSendM1' . (string)time()],
                        ]);

示例响应

  $answer = array(3) {
              ["result"]=>
              array(2) {
                [0]=>
                array(6) {
                  ["error"]=>
                  int(0)
                  ["errorDescription"]=>
                  string(9) "No errors"
                  ["reference"]=>
                  string(22) "successSendM1488547867"
                  ["id"]=>
                  string(6) "174584"
                  ["price"]=>
                  float(0.1)
                  ["currency"]=>
                  string(3) "EUR"
                }
                [1]=>
                array(6) {
                  ["error"]=>
                  int(0)
                  ["errorDescription"]=>
                  string(9) "No errors"
                  ["reference"]=>
                  string(23) "successSendM11488547867"
                  ["id"]=>
                  string(6) "174585"
                  ["price"]=>
                  float(0.1)
                  ["currency"]=>
                  string(3) "EUR"
                }
              }
              ["total_price"]=>
              float(0.2)
              ["currency"]=>
              string(3) "EUR"
            }

查看状态

通过短信ID

$response = $smsClient->getStatusById(214);

通过参考

$response = $smsClient->getStatusByReference(ref1);

通过任务ID

$response = $smsClient->getTaskStatus(214);

示例响应

单个短信响应

  array(11) {
    ["error"]=>
    int(0)
    ["errorDescription"]=>
    string(9) "No errors"
    ["id"]=>
    string(12) "380501111111"
    ["msisdn"]=>
    string(12) "380501111111"
    ["reference"]=>
    string(21) "successSend1488548141"
    ["time_in"]=>
    string(19) "2017-03-03 13:35:42"
    ["time_sent"]=>
    string(19) "2017-03-03 13:35:42"
    ["time_dr"]=>
    string(19) "2017-03-03 13:35:42"
    ["status"]=>
    string(7) "unknown"
    ["price"]=>
    int(0)
    ["currency"]=>
    string(3) "EUR"
  }

###任务短信响应

  array(10) {
    ["originator"]=>
    string(4) "name"
    ["body"]=>
    string(4) "body"
    ["validity"]=>
    int(72)
    ["totalprice"]=>
    float(0.1)
    ["currency"]=>
    string(3) "EUR"
    ["sent"]=>
    int(1)
    ["delivered"]=>
    int(1)
    ["expired"]=>
    int(0)
    ["undeliverable"]=>
    int(0)
    ["unknown"]=>
    int(0)
  }

Viber消息

Viber提供API,可以向世界任何国家的用户发送Viber消息。

BSG 通过一个唯一的随机 ID 识别。您可以使用此 ID 通过提供的端点始终检查消息的状态。

API函数addMessage

将Viber消息添加到队列

参数

to (数组) 收件人msisdn和参考的数组。必需

messages (字符串) Viber消息。必需。

options: (哈希) 包含Viber选项的哈希。

alpha_name (字符串) 消息的发送者。如果没有设置,则使用BSG类中的“viber发送者名称”。如果为空,则为必需

is_promotional (布尔值) 设置此消息是否为促销。默认为true

callback_url (字符串) 递送报告的链接。在消息数组中需要引用id

$viberClient = $bsg->getViberClient();

$response = $viberClient->addMessage([['msisdn' => 380501111111]], 'test');

将图片和链接添加到消息中

$viberClient->addMessage([['msisdn' => msisdn' => 380501111111]], 'test', [
                    "img" => "http://my-cool-webpage.com/logo.png",
                    "caption" => "Join us!",
                    "action" => "http://my-cool-webpage.com"
                ]);

返回:无

API函数sendMessages

发送队列中的所有消息

参数

validity (int) 消息有效的秒数。

tariff (int) 价格表的资费代码。

$viberClient = $bsg->sendMessages();

响应示例

$response = array(3) {
  ["result"]=>
  array(1) {
    [0]=>
    array(5) {
      ["error"]=>
      int(0)
      ["errorDescription"]=>
      string(9) "No errors"
      ["id"]=>
      string(6) "174592"
      ["price"]=>
      int(0)
      ["currency"]=>
      string(3) "EUR"
    }
  }
  ["currency"]=>
  string(3) "EUR"
  ["total_price"]=>
  int(0)
}

查看状态

检索现有消息的信息。此消息可以是已发送或接收的消息。您只需要提供在创建或接收时返回的唯一消息ID。

$response = $viberClient->getStatusById(216);

或通过参考

$response = $api->getStatusByReference("ext_id_19");

示例响应

  $response = array(11) {
                ["error"]=>
                int(0)
                ["errorDescription"]=>
                string(9) "No errors"
                ["id"]=>
                string(12) "380501111111"
                ["msisdn"]=>
                string(12) "380501111111"
                ["reference"]=>
                string(25) "38050111111158b977704621d"
                ["time_in"]=>
                string(19) "2017-03-03 14:02:24"
                ["time_sent"]=>
                string(19) "2017-03-03 14:02:24"
                ["time_dr"]=>
                string(19) "2017-03-03 14:02:24"
                ["status"]=>
                string(7) "expired"
                ["price"]=>
                int(0)
                ["currency"]=>
                string(3) "EUR"
              }

价格

BSG提供API,以获取您账户的价格信息。

API函数“getPrices”

返回所选套餐价格


参数

tariff (整数) 信息类型

HLR价格

$response = $hlrClient->getPrices();

SMS价格

$response = $smsClient->getPrices();

按关税ID的Viber价格

$response = $viberClient->getPrices();

示例响应

$response = array(3) {
                ["error"]=>
                int(0)
                ["errorDescription"]=>
                string(9) "No errors"
                ["prices"]=>
                array(191) {
                  [0]=>
                  array(6) {
                    ["type"]=>
                    string(3) "hlr"
                    ["country"]=>
                    string(0) ""
                    ["country_name"]=>
                    string(15) "Other countries"
                    ["mcc"]=>
                    string(0) ""
                    ["price"]=>
                    string(9) "0.0034000"
                    ["currency"]=>
                    string(3) "EUR"
                  }
                  [1]=>
                  array(6) {
                    ["type"]=>
                    string(3) "hlr"
                    ["country"]=>
                    string(2) "AB"
                    ["country_name"]=>
                    string(8) "Abkhazia"
                    ["mcc"]=>
                    string(3) "289"
                    ["price"]=>
                    string(9) "0.0050000"
                    ["currency"]=>
                    string(3) "EUR"
                  }
                  [2]=>
                  array(6) {
                    ["type"]=>
                    string(3) "hlr"
                    ["country"]=>
                    string(2) "AD"
                    ["country_name"]=>
                    string(7) "Andorra"
                    ["mcc"]=>
                    string(3) "213"
                    ["price"]=>
                    string(9) "0.0025000"
                    ["currency"]=>
                    string(3) "EUR"
                  }
                 }
                }

###测试此SDK还包含用于所有API函数的PHPUnit测试(带有错误检查)。要启动它们,请检查“/tests/TestConfig.php”并更改API密钥、发送者名称和Viber发送者名称的值。之后,只需使用

$ phpunit -c tests/