定制 mtn-sms/mtn-bulksms-php-sdk 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mtn-sms/mtn-bulksms-php-sdk

Composer 安装命令:

composer require mtn-sms/mtn-bulksms-php-sdk

包简介

PHP SDK for MTN API

README 文档

README

Overview

The MTN API SDK provides a PHP wrapper for interacting with the MTN Short Message Service (SMS) API. This SDK simplifies the process of sending outbound SMS messages, managing subscriptions, and handling exceptions.

Installation

You can install the MTN API SDK via Composer. Run the following command in your project directory:

composer mtn-sms/mtn-bulksms-php-sdk

Usage

1. Obtain Access Token

Before using the SDK, you need to obtain an access token using the provided Client Credentials OAuth Flow. You can do this by making a POST request to the token URL with your client credentials.

use MtnApiSdk\AccessToken;

$accessToken = AccessToken::getAccessToken($clientId, $clientSecret);

Replace $clientId and $clientSecret with your actual client credentials.

2. Sending SMS

To send an SMS message, use the SmsSender class.

use MtnApiSdk\SmsSender;

$senderAddress = "MTN";
$receiverAddress = ["23423456789", "23423456790"];
$message = "Hello from MTN API SDK";
$clientCorrelatorId = "123456";
$serviceCode = "11221";
$requestDeliveryReceipt = false;

$response = SmsSender::sendSms($accessToken, $senderAddress, $receiverAddress, $message, $clientCorrelatorId, $serviceCode, $requestDeliveryReceipt);

// Handle response
print_r($response);

3. Managing Subscriptions

You can manage subscriptions using the SubscriptionManager class.

3.1 Subscribe

use MtnApiSdk\SubscriptionManager;

$callbackUrl = "https://example.com/callback";
$targetSystem = "YourSystem";
$serviceCode = "11221";

$response = SubscriptionManager::subscribe($accessToken, $callbackUrl, $targetSystem, $serviceCode);

// Handle response
print_r($response);

3.2 Update Subscription

use MtnApiSdk\SubscriptionManager;

$subscriptionId = "your-subscription-id";
$body = [
    'serviceCode' => '11221',
    'callbackUrl' => 'https://example.com/callback',
    'deliveryReportUrl' => 'https://example.com/delivery-report',
    'targetSystem' => 'YourSystem'
];

$response = SubscriptionManager::updateSubscription($accessToken, $subscriptionId, $body);

// Handle response
print_r($response);

3.3 Delete Subscription

use MtnApiSdk\SubscriptionManager;

$subscriptionId = "your-subscription-id";

$response = SubscriptionManager::deleteSubscription($accessToken, $subscriptionId);

// Handle response
print_r($response);

4. Handling Exceptions

The SDK throws ApiException in case of errors. You can catch and handle these exceptions as follows:

use MtnApiSdk\ApiException;

try {
    // Code that may throw an exception
} catch (ApiException $e) {
    // Handle API exception
    echo "Error occurred: " . $e->getMessage();
}

Adding Credentials

You should store your client credentials securely. It's recommended to use environment variables or a configuration file to store sensitive information.

Using Environment Variables

You can set environment variables in your .env file:

MTN_CLIENT_ID=your-client-id
MTN_CLIENT_SECRET=your-client-secret

Then, you can access these variables in your code:

$clientId = getenv('MTN_CLIENT_ID');
$clientSecret = getenv('MTN_CLIENT_SECRET');

Using Configuration File

You can store your credentials in a configuration file (e.g., config.php):

<?php

return [
    'clientId' => 'your-client-id',
    'clientSecret' => 'your-client-secret'
];

Then, include this file in your code:

$config = include('config.php');
$clientId = $config['clientId'];
$clientSecret = $config['clientSecret'];

Example Test

Here's an example script that demonstrates how to use the MTN API SDK to send an SMS message:

<?php

require_once 'vendor/autoload.php';

use MtnApiSdk\AccessToken;
use MtnApiSdk\SmsSender;
use MtnApiSdk\ApiException;

// Replace these values with your actual credentials
$clientId = getenv('MTN_CLIENT_ID');
$clientSecret = getenv('MTN_CLIENT_SECRET');

// Obtain access token
$accessToken = AccessToken::getAccessToken($clientId, $clientSecret);

// SMS details
$senderAddress = "MTN";
$receiverAddress = ["23423456789", "23423456790"];
$message = "Hello from MTN API SDK";
$clientCorrelatorId = "123456";
$serviceCode = "11221";
$requestDeliveryReceipt = false;

try {
    // Send SMS
    $response = SmsSender::sendSms($accessToken, $senderAddress, $receiverAddress, $message, $clientCorrelatorId, $serviceCode, $requestDeliveryReceipt);

    // Handle response
    print_r($response);
} catch (ApiException $e) {
    // Handle API exception
    echo "Error occurred: " . $e->getMessage();
}

Make sure to replace your-client-id and your-client-secret with your actual MTN client credentials. Additionally, ensure you have set up your environment variables or configuration file to securely store your credentials.

To test this example:

  1. Save the script to a file (e.g., send_sms_example.php).
  2. Set up your client credentials either via environment variables or a configuration file.
  3. Run the script using PHP:
php send_sms_example.php

This script will obtain an access token, send an SMS message using the MTN API SDK, and print the response or handle any exceptions that occur during the process. Adjust the SMS details and error handling as needed for your use case.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please create a pull request or open an issue on GitHub.

License

This SDK is open-source and available under the MIT License. See the LICENSE file for details.

mtn-sms/mtn-bulksms-php-sdk 适用场景与选型建议

mtn-sms/mtn-bulksms-php-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 02 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mtn-sms/mtn-bulksms-php-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mtn-sms/mtn-bulksms-php-sdk 我们能提供哪些服务?
定制开发 / 二次开发

基于 mtn-sms/mtn-bulksms-php-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-02-20