Send SMS with PHP from your own phone.
To send an SMS from PHP, POST a number and a message to https://app.sms8.io/api/v1/messages with your API key in the X-Api-Key header. The curl extension is all you need, in a plain script, WordPress, Laravel or Symfony. The text leaves from the SIM in your own Android phone, with no per-message fee.
Quick start in three steps
From a new account to a delivered text in a few minutes.
Get your API key
Create an SMS8 account, pair an Android phone with the SMS8 app, and copy your key from the API page of the dashboard. Store it in an environment variable called SMS8_API_KEY.
Check the curl extension
PHP 7.4 or newer with the curl extension, which almost every host has. Prefer a class? The dashboard API page offers a ready PHP class with a method per endpoint.
Send your first SMS
Run the function below with your own number. The phone you paired sends the text from its SIM, and you get a message ID back.
How do I send an SMS with PHP?
One small function covers every send. It posts form fields, checks the success flag and returns the decoded answer.
<?php
function sms8_post(string $path, array $fields): array {
$ch = curl_init('https://app.sms8.io/api/v1/' . $path);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['X-Api-Key: ' . getenv('SMS8_API_KEY')],
CURLOPT_POSTFIELDS => http_build_query($fields),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
]);
$raw = curl_exec($ch);
if ($raw === false) throw new RuntimeException(curl_error($ch));
return json_decode($raw, true);
}
$res = sms8_post('messages', [
'number' => '+14155550100',
'message' => 'Hello from PHP and SMS8',
]);
if (!empty($res['success'])) {
echo 'queued as ' . $res['data']['messages'][0]['ID'];
} else {
echo 'refused: ' . $res['error']['code'] . ' ' . $res['error']['message'];
}
Every answer has the same shape: success, data and an error object with a code and a message. Always read the flag, because some refusals, such as no credits left, come back inside a normal response.
Laravel
use Illuminate\Support\Facades\Http;
$res = Http::withHeaders(['X-Api-Key' => env('SMS8_API_KEY')])
->asForm()
->timeout(30)
->post('https://app.sms8.io/api/v1/messages', [
'number' => '+14155550100',
'message' => 'Your order #4412 has shipped.',
])
->json();
On WordPress or WooCommerce you can skip the code: the SMS8 WordPress plugin sends order and form texts for you.
How do I send SMS to many numbers in PHP?
For the same text, put the numbers comma-separated in number. Add option => 1 to rotate across every phone you paired, or option => 2 to rotate across every SIM.
$res = sms8_post('messages', [
'number' => implode(',', ['+14155550100', '+14155550101', '+14155550102']),
'message' => 'The store opens at 9:00 tomorrow.',
'option' => 1,
]);
$ids = array_column($res['data']['messages'], 'ID');
For a different text per person, send a messages field holding a JSON list:
$batch = [
['number' => '+14155550100', 'message' => 'Hi Ana, your table for 4 is ready.'],
['number' => '+14155550101', 'message' => 'Hi Ben, your order #4412 has shipped.'],
];
$res = sms8_post('messages', ['messages' => json_encode($batch)]);
Add schedule with a Unix timestamp, such as time() + 3600, to send later, and prioritize => 1 for codes and alerts. The API has no artificial rate limit, but each SIM should send about one message every 10 to 30 seconds; add phones to send more.
How do I send an MMS with PHP?
Set type to mms and put public image links in attachments, separated by commas. JPG, PNG, GIF and WEBP work, and SMS8 resizes each image to 1,280 pixels.
$res = sms8_post('messages', [
'number' => '+14155550100',
'message' => 'Here is your receipt.',
'type' => 'mms',
'attachments' => 'https://example.com/receipt.jpg,https://example.com/map.png',
]);
More in the guide on sending MMS through an API.
How do I read SMS replies in PHP?
Poll for messages received in the last hour
$res = sms8_post('messages/read', [
'status' => 'Received',
'startTimestamp' => time() - 3600,
'limit' => 100,
]);
$inbox = !empty($res['success']) ? $res['data']['messages'] : [];
foreach ($inbox as $m) {
echo $m['number'] . ': ' . $m['message'] . PHP_EOL;
}
When nothing matches, the answer is success: false with error code 404. Treat that as an empty list. You can also filter by id, deviceID, simSlot or endTimestamp, and page with limit and offset.
Receive replies with a signed webhook
Add an HTTPS URL on the Webhooks page of the dashboard and subscribe it to received messages. Each request carries an X-Signature header: sha256= followed by the hex HMAC-SHA256 of the raw body, keyed with that webhook's own signing secret (not your API key).
<?php
$raw = file_get_contents('php://input');
$expected = 'sha256=' . hash_hmac('sha256', $raw, getenv('SMS8_WEBHOOK_SECRET'));
if (!hash_equals($expected, $_SERVER['HTTP_X_SIGNATURE'] ?? '')) {
http_response_code(401);
exit;
}
$event = json_decode($raw, true);
foreach ($event['data'] ?? [] as $m) {
// your code: insert with a unique index on $m['ID'] so retries are skipped
save_reply($m['ID'], $m['number'], $m['message']);
}
http_response_code(200);
A received message arrives with event, timestamp, userID and a data list where each item has ID, number, message, deviceID and simSlot. Answer within 10 seconds. If your server is down, SMS8 retries on a growing schedule for up to 30 days. The trade-offs are in webhook vs polling for incoming SMS.
Errors and retries
| What you see | Meaning | What to do |
|---|---|---|
| Error code 401 | Missing or wrong API key | Fix the key. Do not retry |
| Error code 400 | Missing or malformed fields | Fix the request. Do not retry |
success: false with a message | Refused, for example no credits left | Log and alert. Retrying will not help |
| Error code 404 on a read | No messages match | Treat as an empty list |
| Error code 500, HTTP 5xx or a timeout | Temporary problem | Retry with backoff |
Retry only on timeouts and server errors, and pass an idempotencyKey so a retry never becomes a second text. SMS8 also returns the first answer for an identical request repeated within five minutes.
function sms8_send_with_retry(string $number, string $text, string $key, int $attempts = 4): array {
for ($i = 0; $i < $attempts; $i++) {
try {
$res = sms8_post('messages', [
'number' => $number, 'message' => $text, 'idempotencyKey' => $key,
]);
if (!empty($res['success'])) return $res['data']['messages'][0];
if (($res['error']['code'] ?? 0) !== 500) {
throw new DomainException($res['error']['message'] ?? 'refused'); // 400, 401, no credits
}
} catch (RuntimeException $e) {
// network error or timeout: retry
}
sleep(2 ** $i); // 1s, 2s, 4s, 8s
}
throw new RuntimeException('SMS8 send failed after retries');
}
sms8_send_with_retry('+14155550100', 'Your order #4412 has shipped.', 'order-4412-shipped');
If the phone later reports a message as failed, put the original back in the queue by its ID instead of sending a new copy. Always pass the id:
sms8_post('messages/resend', ['id' => 128476]);
Every field is in the SMS8 API documentation, and the SMS API guide for developers covers pacing and bulk sends in depth.
Why choose SMS8 for PHP
An SMS API without a number to rent or a meter to watch.
Your own number
Texts leave from the SIM in your own Android phone, so customers see a number they know and can reply to it.
Flat price, unlimited messages
USD 29 a month for one phone, USD 89 for five and USD 229 for twenty, with unlimited SMS and MMS and no per-message fee. Yearly billing is half the price.
No A2P 10DLC filing
Texts go person-to-person from your SIM, so there is no brand or campaign registration to wait for.
Fits any PHP stack
curl and a form post run on shared hosting, Laravel, Symfony or WordPress. A ready PHP class is on the dashboard API page.
Plugins when you want no code
Official WordPress and WooCommerce plugin, GoHighLevel app, Zapier and Make apps, and a Google Sheets add-on use the same account.
Setup in minutes, with a trial
Pair a phone, copy a key and send. The 3-day trial includes 30 messages and needs no card.
Keep going
The reference, the guides and the same examples in other languages.
SMS8 API documentation
Every endpoint, field and webhook event in one place.
Open the docsSMS API guide for developers
Sending, receiving, signatures, retries and pacing, with code in four languages.
Read the guideWebhook vs polling
Which way to receive incoming SMS, and a clean polling loop when you need one.
Read the articleSend SMS with Python
requests and a Flask webhook.
Python examplesSend SMS with Node.js
The sms8-sdk package from npm, plus plain fetch.
Node.js examplesSend SMS with C#
HttpClient and an ASP.NET Core webhook.
C# examplesSend SMS with curl
Test every endpoint from the terminal.
curl examplesYes. The API page of your dashboard offers a ready PHP class with a method for every endpoint, for PHP 7.4 and newer with the curl extension. The plain curl function on this page works just as well.
Use the Http facade with withHeaders for X-Api-Key, asForm, and post number and message to https://app.sms8.io/api/v1/messages. Read the success flag in the JSON answer.
Start the SMS8 trial: 3 days and 30 messages with no card. After that, a flat plan from USD 29 a month covers unlimited SMS and MMS from your own phone.
Compute sha256= plus hash_hmac sha256 of the raw php://input body with your webhook signing secret, and compare it to HTTP_X_SIGNATURE with hash_equals.
The API has no artificial limit. Each SIM should send about one message every 10 to 30 seconds, so add phones and set option to 1 or 2 to rotate across them.
Send your first SMS from PHP
Pair a phone, copy your API key and run the function. 3-day trial with 30 messages, no card needed.