PHP code example of textmagic / textmagic-rest-php-v2
1. Go to this page and download the library: Download textmagic/textmagic-rest-php-v2 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/ */
textmagic / textmagic-rest-php-v2 example snippets
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;
}
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;
}
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;
}
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;
}
use TextMagic\Api\TextMagicApi;
use TextMagic\Models\SendMessageRequest;
use TextMagic\Configuration;
$api->sendMessage($input);
$api->getAllOutboundMessages($page, $limit);
$api->getContact($id);
// ... all other methods unchanged
new SendMessageRequest();
new CreateContactRequest();
// ... all other models unchanged
$config = Configuration::getDefaultConfiguration()
->setUsername('YOUR_USERNAME')
->setPassword('YOUR_API_KEY');
json
"php": "^7.2.5 || ^8.0"
json
"php": "^8.1"
bash
# Check your PHP version
php -v
# Should output: PHP 8.1.x or higher
bash
# macOS (Homebrew)
brew install [email protected]
# Ubuntu/Debian
sudo apt-get install php8.1
# Verify
php -v
bash
# Run your tests
vendor/bin/phpunit
# Or test manually
php your_script.php