sendSms('09171234567', 'Your code is 482913.', 'MYSHOP', ['order_id' => '1042'], 'order-1042-otp'); * echo $res['message']['reference_id']; * * Keep the key in your server's environment, never in code, a browser or a URL. A test key (etxt_test_) * gets the same answers with "sandbox": true and never sends or spends credits. Pass an idempotency key on * sends you may retry: the same key and body within 24 hours returns the first answer, never a second send. * * Docs: https://etxtmo.com/docs */ class EtxtmoException extends Exception { /** @var string|null Stable error code, e.g. insufficient_credits, rate_limited. */ public $errorCode; /** @var int HTTP status. */ public $status; /** @var string|null The request_id to quote to support. */ public $requestId; /** @var int|null Seconds to wait before retrying (429). */ public $retryAfter; public function __construct(string $message, int $status, ?string $errorCode = null, ?string $requestId = null, ?int $retryAfter = null) { parent::__construct($message, $status); $this->status = $status; $this->errorCode = $errorCode; $this->requestId = $requestId; $this->retryAfter = $retryAfter; } } class Etxtmo { private $apiKey; private $baseUrl; private $timeout; public function __construct(string $apiKey, string $baseUrl = 'https://etxtmo.com', int $timeout = 30) { $this->apiKey = $apiKey; $this->baseUrl = rtrim($baseUrl, '/'); $this->timeout = $timeout; } /** POST /api/v1/sms: one message, up to 670 characters. */ public function sendSms(string $recipient, string $content, ?string $senderId = null, ?array $metadata = null, ?string $idempotencyKey = null): array { return $this->request('POST', '/api/v1/sms', $idempotencyKey, array_filter([ 'recipient' => $recipient, 'content' => $content, 'sender_id' => $senderId, 'metadata' => $metadata, ], function ($v) { return $v !== null; })); } /** GET /api/v1/sms/{reference_id}: status pending, retrying, sent, delivered or failed. */ public function getSms(string $referenceId): array { return $this->request('GET', '/api/v1/sms/' . rawurlencode($referenceId)); } /** POST /api/v1/blast: the same message to up to 500 numbers. One blast or bulk request every 2 seconds. */ public function sendBlast(array $recipients, string $content, ?string $senderId = null, ?array $metadata = null, ?string $idempotencyKey = null): array { return $this->request('POST', '/api/v1/blast', $idempotencyKey, array_filter([ 'recipients' => array_values($recipients), 'content' => $content, 'sender_id' => $senderId, 'metadata' => $metadata, ], function ($v) { return $v !== null; })); } /** GET /api/v1/blast/{blast_id} */ public function getBlast(string $blastId): array { return $this->request('GET', '/api/v1/blast/' . rawurlencode($blastId)); } /** POST /api/v1/bulk: a different message per recipient, up to 500. Each item: ['recipient' => ..., 'content' => ..., 'metadata' => [...]]. */ public function sendBulk(array $messages, ?string $senderId = null, ?string $idempotencyKey = null): array { return $this->request('POST', '/api/v1/bulk', $idempotencyKey, array_filter([ 'messages' => array_values($messages), 'sender_id' => $senderId, ], function ($v) { return $v !== null; })); } /** GET /api/v1/bulk/{bulk_id} */ public function getBulk(string $bulkId): array { return $this->request('GET', '/api/v1/bulk/' . rawurlencode($bulkId)); } /** GET /api/v1/account: email, credits, Sender-ID tokens, Sender IDs and status. */ public function account(): array { return $this->request('GET', '/api/v1/account'); } /** * Check a webhook came from eTxtmo.com and is fresh. Same call as before: * $ok = Etxtmo::verifyWebhook(file_get_contents('php://input'), $_SERVER['HTTP_X_ETXTMO_SIGNATURE'] ?? '', 'whsec_...'); * or pass all request headers instead of the signature header (any letter case): * $ok = Etxtmo::verifyWebhook(file_get_contents('php://input'), getallheaders(), 'whsec_...'); * * Since SDK 1.2 the signature covers ".", and a timestamp more than * $toleranceSeconds (default 300) from now is refused, so a captured request cannot be replayed. The timestamp * comes from the optional $timestamp argument, else from the header array, else from * $_SERVER['HTTP_X_ETXTMO_TIMESTAMP']. Without one the check fails. * * @param string|array $signatureOrHeaders The X-eTxtmo-Signature header, or every request header. */ public static function verifyWebhook(string $rawBody, $signatureOrHeaders, string $secret, ?string $timestamp = null, int $toleranceSeconds = 300): bool { $signature = $signatureOrHeaders; if (is_array($signatureOrHeaders)) { $headers = array_change_key_case($signatureOrHeaders, CASE_LOWER); $signature = (string) ($headers['x-etxtmo-signature'] ?? ''); $timestamp = $timestamp ?? (isset($headers['x-etxtmo-timestamp']) ? (string) $headers['x-etxtmo-timestamp'] : null); } $timestamp = trim((string) ($timestamp ?? ($_SERVER['HTTP_X_ETXTMO_TIMESTAMP'] ?? ''))); if (!is_string($signature) || !ctype_digit($timestamp) || abs(time() - (int) $timestamp) > $toleranceSeconds) { return false; } $expected = 'sha256=' . hash_hmac('sha256', $timestamp . '.' . $rawBody, $secret); return hash_equals($expected, trim($signature)); } private function request(string $method, string $path, ?string $idempotencyKey = null, ?array $body = null): array { $headers = [ 'Authorization: Basic ' . base64_encode($this->apiKey . ':'), 'Accept: application/json', 'User-Agent: etxtmo-php/1.2', ]; if ($idempotencyKey !== null) { // Same rule as the server: 1 to 255 visible ASCII characters (no spaces, no CR/LF header injection). if (!preg_match('/^[\x21-\x7E]{1,255}$/', $idempotencyKey)) { throw new InvalidArgumentException('Idempotency key must be 1 to 255 visible ASCII characters.'); } $headers[] = 'Idempotency-Key: ' . $idempotencyKey; } $options = ['method' => $method, 'timeout' => $this->timeout, 'ignore_errors' => true]; if ($body !== null) { $headers[] = 'Content-Type: application/json'; $options['content'] = json_encode($body); } $options['header'] = implode("\r\n", $headers); $raw = @file_get_contents($this->baseUrl . $path, false, stream_context_create(['http' => $options])); if ($raw === false) { throw new EtxtmoException('Could not reach eTxtmo.com.', 0, 'network_error'); } $status = 0; $retryAfter = null; foreach ($http_response_header ?? [] as $line) { if (preg_match('#^HTTP/\S+\s+(\d{3})#', $line, $m)) { $status = (int) $m[1]; } elseif (preg_match('#^Retry-After:\s*(\d+)#i', $line, $m)) { $retryAfter = (int) $m[1]; } } $data = json_decode($raw, true); if ($status >= 400 || !is_array($data)) { throw new EtxtmoException( is_array($data) && isset($data['error']) ? $data['error'] : 'Unexpected response.', $status, is_array($data) ? ($data['code'] ?? null) : null, is_array($data) ? ($data['request_id'] ?? null) : null, $retryAfter ); } return $data; } }