PHP code example of sendimessage / sdk

1. Go to this page and download the library: Download sendimessage/sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sendimessage / sdk example snippets


use SendImessage\Client;

$client = new Client(
    apiKey: getenv('SENDIMESSAGE_KEY'),
    apiSecret: getenv('SENDIMESSAGE_SECRET'),
);

use SendImessage\Client;

$client = new Client(apiKey: '...', apiSecret: '...');

$msg = $client->sendMessage(number: '+15551234567', content: 'Hello from SendiMessage!');

echo $msg['message_handle'], ' ', $msg['status']; // "…" "QUEUED"

$msg = $client->sendMessage(
    number: '+15551234567',  // E.164 phone number, or an email for iMessage
    content: 'Your order has shipped 🎉',
);

$lines = $client->listLines();
$client->sendMessage(
    number: '+15551234567',
    content: 'Hi!',
    lineHandle: $lines['lines'][0]['line_handle'],
);

$url = $client->uploadMedia('./photo.jpg');

$client->sendMessage(number: '+15551234567', mediaUrl: $url);

$client->sendMessage(
    number: '+15551234567',
    content: 'Reminder: appointment at 3pm',
    scheduledAt: '2026-08-10T14:30:00Z',                    // ISO 8601, deliver later
    statusCallback: 'https://example.com/hooks/status',     // POSTed the final status
);

$status = $client->getStatus($msg['message_handle']);
echo $status['status']; // QUEUED | SENT | ERROR

$before = null;
do {
    $page = $client->listMessages(['number' => '+15551234567', 'limit' => 50, 'before' => $before]);
    foreach ($page['messages'] as $m) {
        echo $m['date_sent'], $m['is_outbound'] ? ' → ' : ' ← ', $m['content'], PHP_EOL;
    }
    $before = $page['next_before'];
} while ($before !== null);

$one = $client->getMessage($handle); // single history message

$result = $client->lookup('+15551234567');
echo $result['service']; // "iMessage" | "SMS"
echo $result['cached'];  // true when answered from the capability cache

$client->createContact(['number' => '+15551234567', 'first_name' => 'Jane', 'last_name' => 'Doe']);
$client->updateContact('+15551234567', ['company_name' => 'Acme Inc.']);
$contact = $client->getContact('+15551234567'); // number or contact_handle
$all = $client->listContacts(['limit' => 100]);
$client->deleteContact('+15551234567');
$client->recheckContact('+15551234567'); // re-run the iMessage/SMS check now

$client->setOptOut('+15551234567', true);   // opt out
$client->setOptOut('+15551234567', false);  // opt back in

$lines = $client->listLines();
$client->updateLine($lineHandle, label: 'Support line', isActive: true);

$client->setCallForwarding($lineHandle, '+15559876543'); // request forwarding
$client->setCallForwarding($lineHandle, null);           // request turning it off
$state = $client->getCallForwarding($lineHandle);        // current + pending

$convos = $client->listConversations(['per_page' => 20]);
$msgs = $client->listConversationMessages($conversationHandle, ['per_page' => 50]);
$client->markConversationRead($conversationHandle);

$client->createWebhook(
    url: 'https://example.com/hooks/sendimessage',
    events: ['receive', 'outbound'],        // default: every event
    secret: getenv('WEBHOOK_SECRET'),       // enables the signature header
);

$hooks = $client->listWebhooks();
$client->replaceWebhooks([...]);            // swap the whole set atomically
$client->deleteWebhook('https://example.com/hooks/sendimessage');

use SendImessage\Client;

// The raw body is 

$ok = Client::verifyWebhookSignature(
    secret: getenv('WEBHOOK_SECRET'),
    rawBody: $rawBody,
    signature: $_SERVER['HTTP_X_SMSBRIDGE_SIGNATURE'] ?? '',
);
if (!$ok) {
    http_response_code(401);
    exit;
}

$event = json_decode($rawBody, true);
// ... handle event

use SendImessage\ApiException;

try {
    $client->sendMessage(number: '+15551234567', content: 'hi');
} catch (ApiException $e) {
    error_log($e->getCode() . ' ' . $e->getMessage());
}

$client = new Client(
    apiKey: '...',
    apiSecret: '...',
    baseUrl: 'https://api.sendimessage.com/v1', // default; override for testing
    timeout: 30,                                // seconds, per request
);

$client->sendMessage('+15551234567', content: 'Your code is 1234', service: 'sms');