This library provides you with an easy way of sending SMS and receiving replies by integrating the TextMagic SMS Gateway into your PHP application.
TextMagic's application programming interface (API) provides the communication link between your application and TextMagic's SMS Gateway, allowing you to send and receive text messages and to check the delivery status of text messages you've already sent.
- PHP 8.1 or later
- Composer for dependency management
- cURL extension
- JSON extension
- mbstring extension
Install the SDK using Composer:
composer require textmagic/textmagic-rest-php-v2Or add it to your composer.json:
{
"require": {
"textmagic/textmagic-rest-php-v2": "^3.0.43870"
}
}Then run:
composer installDownload the files and include autoload.php:
require_once(__DIR__ . '/vendor/autoload.php');<?php
require_once(__DIR__ . '/vendor/autoload.php');
use TextMagic\Api\TextMagicApi;
use TextMagic\Configuration;
use GuzzleHttp\Client;
// Get your credentials from: https://app.textmagic.com/settings/api
$config = Configuration::getDefaultConfiguration()
->setUsername('YOUR_USERNAME')
->setPassword('YOUR_API_KEY');
$api = new TextMagicApi(new Client(), $config);
// Test connection
try {
$result = $api->ping();
echo "Ping successful: " . $result->getPing() . PHP_EOL;
} catch (\TextMagic\ApiException $e) {
echo 'API Error: ' . $e->getMessage() . PHP_EOL;
echo 'HTTP Code: ' . $e->getCode() . PHP_EOL;
} catch (\Exception $e) {
echo 'Exception when calling TextMagicApi->ping: ', $e->getMessage(), PHP_EOL;
}<?php
use TextMagic\Models\SendMessageRequest;
$input = new SendMessageRequest();
$input->setText('Hello from TextMagic PHP SDK!');
$input->setPhones('+19993322111,+19993322110');
try {
$result = $api->sendMessage($input);
echo "Message sent! Session ID: " . $result->getId() . PHP_EOL;
} catch (\TextMagic\ApiException $e) {
echo 'Failed to send message: ' . $e->getMessage() . PHP_EOL;
} catch (\Exception $e) {
echo 'Exception when calling TextMagicApi->sendMessage: ', $e->getMessage(), PHP_EOL;
}<?php
try {
$result = $api->getAllOutboundMessages(1, 10);
foreach ($result->getResources() as $message) {
echo "Message ID: " . $message->getId() . PHP_EOL;
echo "Text: " . $message->getText() . PHP_EOL;
echo "Status: " . $message->getStatus() . PHP_EOL;
}
} catch (\TextMagic\ApiException $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
} catch (\Exception $e) {
echo 'Exception when calling TextMagicApi->getAllOutboundMessages: ', $e->getMessage(), PHP_EOL;
}<?php
try {
$file = new \SplFileObject("avatar.jpg");
$result = $api->uploadListAvatar($file, 3223); // 3223 number here it is sample list ID
echo "Avatar uploaded successfully!" . PHP_EOL;
} catch (\TextMagic\ApiException $e) {
echo 'Upload failed: ' . $e->getMessage() . PHP_EOL;
} catch (\Exception $e) {
echo 'Exception when calling TextMagicApi->uploadListAvatar: ', $e->getMessage(), PHP_EOL;
}v2.x:
"php": "^7.2.5 || ^8.0"v3.x:
"php": "^8.1"Action Required: Upgrade your PHP version to 8.1 or later.
# Check your PHP version
php -v
# Should output: PHP 8.1.x or higherv2.x:
"phpunit/phpunit": "^4.8"
"guzzlehttp/guzzle": "^7.2"v3.x:
"phpunit/phpunit": "^10.0"
"guzzlehttp/guzzle": "^7.3"Note: PHPUnit 10 is required for PHP 8.1 compatibility. PHPUnit 8 and 9 do not support PHP 8.1.
✅ Namespaces - No changes required:
use TextMagic\Api\TextMagicApi;
use TextMagic\Models\SendMessageRequest;
use TextMagic\Configuration;✅ API Methods - All methods remain the same:
$api->sendMessage($input);
$api->getAllOutboundMessages($page, $limit);
$api->getContact($id);
// ... all other methods unchanged✅ Model Classes - All models remain the same:
new SendMessageRequest();
new CreateContactRequest();
// ... all other models unchanged✅ Configuration - Configuration setup remains the same:
$config = Configuration::getDefaultConfiguration()
->setUsername('YOUR_USERNAME')
->setPassword('YOUR_API_KEY');-
Upgrade PHP to 8.1+
# macOS (Homebrew) brew install php@8.1 # Ubuntu/Debian sudo apt-get install php8.1 # Verify php -v
-
Update Composer Dependencies
# Update composer.json composer require textmagic/textmagic-rest-php-v2:^3.0 # Or update all dependencies composer update
-
Test Your Application
# Run your tests vendor/bin/phpunit # Or test manually php your_script.php
| Feature | v2.x | v3.x | Compatible? |
|---|---|---|---|
| PHP 7.2-7.4 | ✅ | ❌ | ❌ No |
| PHP 8.0 | ✅ | ❌ | ❌ No |
| PHP 8.1+ | ✅ | ✅ | ✅ Yes |
| API Methods | Same | Same | ✅ Yes |
| Models | Same | Same | ✅ Yes |
| Configuration | Same | Same | ✅ Yes |
The library is available as open source under the terms of the MIT License.