Send SMS with PHP

To send an SMS from PHP, POST a JSON body to the curl-sms API with the cURL extension. It works on any PHP 7.4+ host without extra packages.

Example

<?php

function send_sms(string $receiver, string $body): array
{
    $ch = curl_init('https://curl-sms.com/api/sms/send');
    curl_setopt_array($ch, [
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT => 10,
        CURLOPT_HTTPHEADER => [
            'Authorization: Bearer ' . getenv('CURLSMS_KEY'),
            'Content-Type: application/json',
        ],
        CURLOPT_POSTFIELDS => json_encode(['receiver' => $receiver, 'body' => $body]),
    ]);
    $response = curl_exec($ch);
    if ($response === false) {
        throw new RuntimeException('Network error: ' . curl_error($ch));
    }
    $status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
    $data = json_decode($response, true);
    if ($status !== 202) {
        throw new RuntimeException($data['error']['code'] . ': ' . $data['error']['message']);
    }
    return $data;
}

$sms = send_sms('+4915123456789', 'Hello from PHP');
echo $sms['id'], ' ', $sms['status'], PHP_EOL;

Set CURLSMS_KEY to a test key (cs_test_…) while you build: the API checks everything but sends nothing and charges nothing.

What you get back

202 Accepted with id, status (queued at first), segments and price. Errors arrive as {"error": {"code", "message"}} with a fitting HTTP status, for example 402 insufficient_balance. All codes: error codes.

Tips

  • Laravel: Http::withToken($key)->post('https://curl-sms.com/api/sms/send', [...]) does the same in one line.
  • Shop orders: send an Idempotency-Key header such as order-4711-shipped. Retries with the same key never send a second SMS.
  • Umlauts and emoji: json_encode handles UTF-8 for you. Emoji reduce a part to 70 characters, so check long texts with POST /api/sms/estimate.
  • Status updates: instead of polling, receive delivery reports via webhook.