infobipmagentosdkorg/infobip-sdk-ecommerce

用于调用 Infobip API 的 PHP 库

3.1.2 2022-04-12 14:38 UTC

This package is not auto-updated.

Last update: 2024-09-26 01:55:49 UTC


README

Packegist MIT License

这是一个 Infobip API 的 PHP 客户端,您可以将它作为依赖项添加到您的应用程序中,以使用 Infobip APIs。要使用此客户端,您需要一个 Infobip 帐户。如果没有,您可以在这里创建一个免费试用帐户。

基于 OpenAPI 规范构建,由 OpenAPI Generator 提供支持。

Infobip

目录

文档

Infobip API 文档可在此处找到。

基本信息

对于 infobip-api-php-client 版本,我们使用 语义化版本控制

MIT 许可证 下发布。

PHP 版本

所有高于 7.2 的版本

安装

使用 Composer

要开始使用此库,将其添加到您的 composer.json 文件中,如下所示。

"require": {
	"infobip/infobip-api-php-client": "3.0.0"
}

然后只需运行 composer install 下载依赖项。

不使用 Composer

如果您的环境阻止您使用 composer,您可以手动下载此软件包及其所有依赖项,并在代码中引用它们。但是,有一些解决方案可以自动化此过程。其中之一是 php-download 在线工具。您可以使用它查找预组成的 infobip 客户端软件包,从那里下载它并在项目中使用,而无需手动收集依赖项。

快速入门

初始化配置和 HTTP 客户端

我们支持多种身份验证方法,例如,您可以使用 API Key Header,在这种情况下,API_KEY_PREFIX 将为 "App"。

您可以通过 网络界面创建 API_KEY

要查看您的 URL_BASE_PATH,请使用您的 Infobip 凭证登录到 Infobip API 文档 中心。

    $configuration = (new Configuration())
        ->setHost(URL_BASE_PATH)
        ->setApiKeyPrefix('Authorization', API_KEY_PREFIX)
        ->setApiKey('Authorization', API_KEY);

    $client = new GuzzleHttp\Client();

发送短信

发送短信消息的简单示例。

    $sendSmsApi = new SendSMSApi($client, $configuration);
    $destination = (new SmsDestination())->setTo('41793026727');
    $message = (new SmsTextualMessage())
        ->setFrom('InfoSMS')
        ->setText('This is a dummy SMS message sent using infobip-api-php-client')
        ->setDestinations([$destination]);
    $request = (new SmsAdvancedTextualRequest())
        ->setMessages([$message]);
    try {
        $smsResponse = $sendSmsApi->sendSmsMessage($request);
    } catch (Throwable $apiException) {
        // HANDLE THE EXCEPTION
    }

ApiException 对象中提供的字段是 code,它表示 HTTP 响应代码,以及 responseHeadersresponseBody。您还可以使用 getResponseObject 方法获取反序列化的响应体。

    $apiException->getCode();
    $apiException->getResponseHeaders();
    $apiException->getResponseBody();
    $apiException->getResponseObject();

此外,您还可以从 SmsResponse 对象中提取 bulkIdmessageId(s),并使用它们获取特定消息或批次的投递报告。只有当您向多个目标地址发送消息或单个请求中包含多个消息时,才会收到批 ID。

    $bulkId = $smsResponse->getBulkId();
    $messageId = $smsResponse->getMessages()[0]->getMessageId();

接收短信消息投递报告

对于您发送的每条短信,我们都可以实时发送给您一条投递报告。您需要做的就是指定您的端点,例如,当在 SmsTextualMessagenotifyUrl 字段中发送短信时,指定 https://{yourDomain}/delivery-reports,或者通过联系我们的支持团队订阅报告。

您可以使用库中的数据模型和预配置的 \Infobip\ObjectSerializer 序列化器。

Webhook实现示例

    use \Infobip\ObjectSerializer;

    $data = file_get_contents("php://input");
    $type = '\Infobip\Model\SmsDeliveryResult';
    $deliveryReports = ObjectSerializer::deserialize($data, $type);

    foreach ($deliveryReports->getResults() as $report) {
        echo $report->getMessageId() . " - " . $report->getStatus()->getName() . "\n";
    }

如果您想使用自己的序列化器,请注意支持的日期格式

获取投递报告

如果您因任何原因无法在端点接收实时投递报告,可以使用messageIdbulkId来获取它们。每次请求将返回一批投递报告 - 只返回一次。

    $numberOfReportsLimit = 10;
    $deliveryReports = $sendSmsApi->getOutboundSmsMessageDeliveryReports($bulkId, $messageId, $numberOfReportsLimit);
    foreach ($deliveryReports->getResults() as $report) {
        echo $report->getMessageId() . " - " . $report->getStatus()->getName() . "\n";
    }

Unicode & SMS预览

Infobip API支持Unicode字符并自动检测编码。Unicode和非标准GSM字符会占用额外空间,避免不愉快的惊喜,并检查不同的消息配置如何影响您的消息文本、字符数和消息部分。

    $previewResponse = $sendSmsApi->previewSmsMessage((new SmsPreviewRequest())
        ->setText("Let's see how many characters will remain unused in this message."));
    echo $previewResponse;

接收短信

如果您想从您的订阅者接收短信,我们可以将它们实时发送给您。当您购买并配置一个可以接收短信的号码时,请按照以下说明指定您的端点此处,例如https://{yourDomain}/incoming-sms。Webhook实现示例

    use \Infobip\ObjectSerializer;

    $data = file_get_contents("php://input");
    $type = '\Infobip\Model\SmsInboundMessageResult';
    $messages = ObjectSerializer::deserialize($data, $type);

    foreach ($messages->getResults() as $message) {
        echo $message-> getFrom() . " - " . $message-> getCleanText() . "\n";
    }

双因素认证(2FA)

有关2FA快速入门指南,请查看这些示例

Infobip\SendWhatsappApi

所有URI相对于https:///whatsapp/1

方法HTTP请求描述
sendWhatsappMessage()POST /whatsapp/1/message/template发送WhatsApp消息。

sendWhatsappMessage()

sendWhatsappMessage($pi_integrator, $pi_platform, $whatsapp_message): \Infobip\Model\WhatsappResponse

发送WhatsApp消息。

使用预定义的WhatsApp模板发送WhatsApp消息

示例

<?php
require_once(__DIR__ . '/vendor/autoload.php');



$apiInstance = new Infobip\Api\SendWhatsappApi(
    // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
    // This is optional, `GuzzleHttp\Client` will be used as default.
    new GuzzleHttp\Client()
);
$pi_integrator = 56; // int
$pi_platform = 'pi_platform_example'; // string
$whatsapp_message = new \Infobip\Model\WhatsappMessage(); // \Infobip\Model\WhatsappMessage

try {
    $result = $apiInstance->sendWhatsappMessage($pi_integrator, $pi_platform, $whatsapp_message);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling SendWhatsappApi->sendWhatsappMessage: ', $e->getMessage(), PHP_EOL;
}

参数

名称类型描述备注
pi_integratorint[可选]
pi_platformstring[可选]
whatsapp_message\Infobip\Model\WhatsappMessage[可选]

返回类型

\Infobip\Model\WhatsappResponse

认证

无需认证

HTTP请求头

  • Content-Type: application/json
  • Accept: application/json

[返回顶部] [返回API列表] [返回模型列表] [返回README]

Infobip\WhatsappTemplatesApi

所有URI相对于https:///whatsapp/1

方法HTTP请求描述
getTemplates()GET /whatsapp/1/senders/{sender}/templates获取模板列表。

getTemplates()

getTemplates($sender): \Infobip\Model\WhatsappTemplateResponse

获取模板列表。

示例

<?php
require_once(__DIR__ . '/vendor/autoload.php');



$apiInstance = new Infobip\Api\WhatsappTemplatesApi(
    // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
    // This is optional, `GuzzleHttp\Client` will be used as default.
    new GuzzleHttp\Client()
);
$sender = 56; // int | Sender number wit country code

try {
    $result = $apiInstance->getTemplates($sender);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling WhatsappTemplatesApi->getTemplates: ', $e->getMessage(), PHP_EOL;
}

参数

名称类型描述备注
senderint带国家代码的发送者号码

返回类型

\Infobip\Model\WhatsappTemplateResponse

认证

无需认证

HTTP请求头

  • Content-Type: 未定义
  • Accept: application/json

[返回顶部] [返回API列表] [返回模型列表] [返回README]

寻求帮助

如有任何问题或功能请求,请随时在存储库中提交问题。关于拉取请求,请查看相关的CONTRIBUTING 文件 - 简而言之,我们不会合并任何拉取请求,此代码是自动生成的。

如果您有需要我们立即关注的问题,请随时通过support@infobip.com联系。