PHP code example of strongmb / strongmb-php

1. Go to this page and download the library: Download strongmb/strongmb-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

strongmb / strongmb-php example snippets


use Strongmb\Strongmb;

$strongmb = new Strongmb('your-api-key');

$response = $strongmb->data->purchase(
    phone:       '08012345678',
    productCode: 'smb_mtn_sme_1gb_30days',
    reference:   'MYAPP' . strtoupper(uniqid()),
);

if ($response->successful()) {
    echo 'Data sent! Reference: ' . $response->data()['reference'];
} elseif ($response->processing()) {
    echo 'Processing — poll by reference to check status.';
} elseif ($response->failed()) {
    echo 'Transaction failed. Contact support if wallet was debited.';
}

// Live mode
$strongmb = new Strongmb(apiKey: 'your-api-key');

// Sandbox / test mode
$strongmb = new Strongmb(apiKey: 'your-api-key', sandbox: true);

// List all available data plans and product codes
$response = $strongmb->data->plans();
$billers  = $response->data()['billers'];

// Purchase a data plan
$response = $strongmb->data->purchase(
    phone:       '08012345678',
    productCode: 'smb_mtn_sme_1gb_30days',
    reference:   'MYAPP_UNIQUE_REF_001',
);

// List all airtime providers and product codes
$response = $strongmb->airtime->plans();

// Top up airtime
$response = $strongmb->airtime->purchase(
    phone:       '08012345678',
    productCode: 'smb_mtn_vtu',
    amount:      '100',
    reference:   'MYAPP_UNIQUE_REF_002',
);

// Authenticated user profile
$response = $strongmb->account->user();

// Wallet balances
$response = $strongmb->account->wallets();

// Transaction history
$response = $strongmb->account->transactions();

// Single transaction by reference
$response = $strongmb->account->transaction('MYAPP_UNIQUE_REF_001');

$response->ok();          // bool   — true when the API status is successful
$response->message();     // string — human-readable message from the API
$response->data();        // array  — the response payload
$response->code();        // string — machine-readable code (e.g. TRANSACTION_SUCCESSFUL)
$response->traceId();     // string — use this when contacting support
$response->httpStatus();  // int    — HTTP status code
$response->toArray();     // array  — full raw response

// Purchase transaction helpers
$response->successful();  // true when code === TRANSACTION_SUCCESSFUL
$response->processing();  // true when still being processed — poll by reference
$response->failed();      // true when transaction failed — wallet may have been debited

if ($response->successful()) {
    $data = $response->data();
    echo 'Done! Reference: ' . $data['reference'];

} elseif ($response->processing()) {
    // Request accepted but not yet fulfilled.
    // Poll account->transaction($reference) until it resolves.
    echo 'Processing — reference: ' . $response->data()['reference'];

} elseif ($response->failed()) {
    // Transaction was attempted but failed.
    // Provide the trace ID to support if the wallet was debited.
    echo 'Failed. Trace ID: ' . $response->traceId();
}

use Strongmb\Exceptions\ApiException;
use Strongmb\Exceptions\AuthException;
use Strongmb\Exceptions\StrongmbException;

try {
    $response = $strongmb->data->purchase(...);

} catch (AuthException $e) {
    // HTTP 401 — invalid or missing API key
    echo 'Invalid API key.';

} catch (ApiException $e) {
    // HTTP 4xx / 5xx — the API returned an error
    echo $e->getMessage();     // Human-readable message
    echo $e->getApiCode();     // e.g. ERR_INSUFFICIENT_BALANCE
    echo $e->getHttpStatus();  // e.g. 402
    echo $e->getTraceId();     // Send this to [email protected]

} catch (StrongmbException $e) {
    // Network error or unexpected failure
    echo 'Network error: ' . $e->getMessage();
}
bash
composer