Developers · PHP

PHP SMS integration — REST API samples

Ready-to-use PHP HTTPS/JSON examples for SMS Gateway. Any HTTP client works—you do not need a proprietary package SDK.

PHP 7.4+

Compatible

REST

HTTPS + JSON

Samples

Copy into your app

Overview

Quick Start Overview

Everything you need to add SMS Gateway to your PHP app

01

HTTPS + JSON samples

Copy-paste PHP cURL and request patterns for the REST API

02

Easy integration

Works with any PHP 7.4+ stack that can open HTTPS requests

03

Error-aware samples

Examples include basic error handling for production-shaped code

04

Framework-agnostic

Use with Laravel, CodeIgniter, Symfony, or plain PHP

Code

PHP Code Examples

Copy-paste PHP code examples for all SMS Gateway features

Send SMS

POST /api/v1/messages with Bearer token and JSON { to, text }

Basic
<?php
// SMS Gateway — POST /api/v1/messages (Bearer JSON). Docs: https://docs.sms-gateway.app/
$url = 'https://app.sms-gateway.app/api/v1/messages';
$key = getenv('SMS_GATEWAY_API_KEY'); // never commit real keys
$payload = json_encode([
    'to' => ['+14155552671'], // E.164 with +
    'text' => 'Hello from SMS Gateway!',
    'type' => 'sms',
]);

$ctx = stream_context_create([
    'http' => [
        'method' => 'POST',
        'header' => implode("\r\n", [
            'Authorization: Bearer ' . $key,
            'Content-Type: application/json',
            'Idempotency-Key: ' . bin2hex(random_bytes(16)),
        ]),
        'content' => $payload,
        'timeout' => 30,
        'ignore_errors' => true,
    ],
]);
$response = file_get_contents($url, false, $ctx);
$result = json_decode($response, true);

if (!empty($result['messages'][0]['id'])) {
    $id = $result['messages'][0]['id'];
    echo 'Queued. Message ID: ' . $id;
} else {
    $err = $result['error']['code'] ?? 'unknown';
    echo 'Error: ' . $err . ' — ' . ($result['error']['message'] ?? $response);
}

Bulk SMS

Several E.164 numbers in one POST /messages (to[])

Advanced
<?php
$url = 'https://app.sms-gateway.app/api/v1/messages';
$key = getenv('SMS_GATEWAY_API_KEY');
$payload = json_encode([
    'to' => ['+14155552671', '+14155552672'],
    'text' => 'Hello from SMS Gateway!',
    'type' => 'sms',
    'dedupe' => true,
]);
$ctx = stream_context_create([
    'http' => [
        'method' => 'POST',
        'header' => "Authorization: Bearer $key\r\nContent-Type: application/json\r\n",
        'content' => $payload,
        'timeout' => 30,
        'ignore_errors' => true,
    ],
]);
echo file_get_contents($url, false, $ctx);

Webhook Handler

Verify X-SmsGateway-Signature then handle message.received

Advanced
<?php
header('Content-Type: application/json');

$secret = getenv('SMS_GATEWAY_WEBHOOK_SECRET');
$timestamp = $_SERVER['HTTP_X_SMSGATEWAY_TIMESTAMP'] ?? '';
$sigHeader = $_SERVER['HTTP_X_SMSGATEWAY_SIGNATURE'] ?? '';
$eventId = $_SERVER['HTTP_X_SMSGATEWAY_EVENT_ID'] ?? '';
$body = file_get_contents('php://input');

if ($timestamp === '' || $sigHeader === '' || $secret === '') {
    http_response_code(400);
    echo json_encode(['error' => 'missing_signature']);
    exit;
}

if (abs(time() - (int) $timestamp) > 300) {
    http_response_code(401);
    echo json_encode(['error' => 'stale_timestamp']);
    exit;
}

$expected = 'v1=' . hash_hmac('sha256', $timestamp . '.' . $body, $secret);
$ok = false;
foreach (preg_split('/\s+/', $sigHeader) as $candidate) {
    if (hash_equals($expected, $candidate)) {
        $ok = true;
        break;
    }
}
if (!$ok) {
    http_response_code(401);
    echo json_encode(['error' => 'bad_signature']);
    exit;
}

// Deduplicate on X-SmsGateway-Event-Id (retries and replays reuse it).
$data = json_decode($body, true) ?: [];
$type = $data['type'] ?? '';
$message = $data['data']['message'] ?? [];

if ($type === 'message.received') {
    $from = $message['number'] ?? '';
    $text = $message['text'] ?? '';
    error_log("Inbound SMS $eventId from $from: $text");
}

http_response_code(200);
echo json_encode(['ok' => true]);
Resources

Additional Resources

Everything you need to get started with PHP SMS integration

01

API Documentation

REST endpoints, auth, webhooks, and status handling

Open guide
02

Webhook guide

Inbound SMS and event callbacks for your PHP app

Open guide
03

Pair a device first

Install the Android app and connect a SIM before API sends

Open guide
Get started

Ready to Integrate?

Install the Android app, pair a SIM device, then paste the API samples into your PHP backend