PHPackages                             mtn-sms/mtn-bulksms-php-sdk - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. mtn-sms/mtn-bulksms-php-sdk

ActiveLybrary[API Development](/categories/api)

mtn-sms/mtn-bulksms-php-sdk
===========================

PHP SDK for MTN API

v0.0.2(2y ago)203MITPHPPHP ^7.2

Since Feb 20Pushed 2y ago1 watchersCompare

[ Source](https://github.com/kazashim/mtn-bulksms-php-sdk)[ Packagist](https://packagist.org/packages/mtn-sms/mtn-bulksms-php-sdk)[ RSS](/packages/mtn-sms-mtn-bulksms-php-sdk/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

MTN API SDK
===========

[](#mtn-api-sdk)

Overview
--------

[](#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
------------

[](#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
-----

[](#usage)

### 1. Obtain Access Token

[](#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

[](#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

[](#3-managing-subscriptions)

You can manage subscriptions using the `SubscriptionManager` class.

#### 3.1 Subscribe

[](#31-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

[](#32-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

[](#33-delete-subscription)

```
use MtnApiSdk\SubscriptionManager;

$subscriptionId = "your-subscription-id";

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

// Handle response
print_r($response);
```

### 4. Handling Exceptions

[](#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
------------------

[](#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

[](#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

[](#using-configuration-file)

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

```
