Skip to content
Connect Media Connect Media

How to Send SMS with PHP

This tutorial shows how to send SMS from any PHP application — Laravel, CodeIgniter, WordPress or plain PHP — using the Connect Media bulk SMS API.

PHP

1. Get an API key

Create an account, then go to Profile → API keys. Store the key in an environment variable called CM_API_KEY; never commit it to Git.

2. Send an SMS

<?php
function cm_send_sms(string $to, string $message, ?string $sender = null): array
{
    $payload = ['action' => 'send', 'to' => $to, 'message' => $message];
    if ($sender) {
        $payload['sender'] = $sender; // registered sender ID, max 11 chars
    }

    $ch = curl_init('https://dashboard.connectmedia.co.ke/api.php');
    curl_setopt_array($ch, [
        CURLOPT_POST           => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT        => 15,
        CURLOPT_HTTPHEADER     => [
            'Authorization: Bearer ' . getenv('CM_API_KEY'),
            'Content-Type: application/json',
        ],
        CURLOPT_POSTFIELDS     => json_encode($payload),
    ]);
    $body = curl_exec($ch);
    $err  = curl_error($ch);
    curl_close($ch);

    if ($body === false) {
        throw new RuntimeException('SMS request failed: ' . $err);
    }
    return json_decode($body, true) ?? [];
}

$res = cm_send_sms('254712345678', 'Your order #1042 has shipped.');
if (($res['code'] ?? '') !== '201') {
    error_log('SMS not accepted: ' . ($res['message'] ?? 'unknown'));
}

A code of 201 means the message was accepted. Other codes tell you what went wrong — for example 101 for insufficient balance or 104 for no valid recipients.

3. Format Kenyan phone numbers

Customers type numbers in many ways. Normalise them to 2547XXXXXXXX / 2541XXXXXXXX before sending:

function cm_normalise_ke(string $raw): ?string
{
    $d = preg_replace('/\D/', '', $raw);          // keep digits only
    if (str_starts_with($d, '0'))   $d = '254' . substr($d, 1);
    if (strlen($d) === 9)           $d = '254' . $d;  // 712345678
    return preg_match('/^254[17]\d{8}$/', $d) ? $d : null;
}

4. Sending from WordPress

Inside WordPress, use the built-in HTTP API instead of cURL:

$response = wp_remote_post('https://dashboard.connectmedia.co.ke/api.php', [
    'timeout' => 15,
    'headers' => [
        'Authorization' => 'Bearer ' . CM_API_KEY, // define in wp-config.php
        'Content-Type'  => 'application/json',
    ],
    'body'    => wp_json_encode([
        'action'  => 'send',
        'to'      => '254712345678',
        'message' => 'Thanks for registering!',
    ]),
]);
$res = json_decode(wp_remote_retrieve_body($response), true);

Building a shop? See WooCommerce order SMS notifications.

Tips

  • Send bulk messages by passing comma-separated numbers in to.
  • Queue SMS (e.g. Laravel queues) so a slow network call never delays your page.
  • Don’t retry blindly after a timeout — a retry can send the message twice.

Other languages: Python · Node.js. Full reference: API docs.

Get started

Create a Connect Media account — SMS from KES 1.0, no minimum top-up, credit that never expires, and one account for Safaricom, Airtel and Telkom. Call +254 707 339 945 or email info@connectmedia.co.ke.

Frequently asked questions

How do I send SMS from PHP?

Make an HTTPS POST to the Connect Media API with your API key in the Authorization header and a JSON body containing action send, the number and the message.

Can I send bulk SMS from PHP in one request?

Yes. Put several numbers, separated by commas, in the to field.

Does this work with Laravel?

Yes. The same request works in Laravel, using cURL or Laravel's HTTP client.