Tuesday 14 August 2018

PHP Text Messaging with TextMagic

A few months back I wrote a shocking expose about how sending text messages with PHPwas as simple as a simple call to mail(). The only drawback to using the mail method is that you need know the carrier for a given phone number -- a humiliating question to ask anyone you plan to advertise to. Luckily there's an simple, effective web service available called TextMagic. TextMagic provides an API for a variety of languages (PHP, Perl, Python, Ruby, Java, etc.) and is very easy to configure.

The PHP
Once you have a TextMagic account and you've downloaded the API helper provided by TextMagic, all you need to do is create a bit of custom code to send your text message:

// Include the TextMagic PHP lib
require('textmagic-sms-api-php/TextMagicAPI.php');

// Set the username and password information
$username = 'myusername';
$password = 'mypassword';

// Create a new instance of TM
$router = new TextMagicAPI(array(
'username' => $username,
'password' => $password
));

// Send a text message to '999-123-4567'
$result = $router->send('Wake up!', array(9991234567), true);

// result:  Result is: Array ( [messages] => Array ( [19896128] => 9991234567 ) [sent_text] => Wake up! [parts_count] => 1 )
The result of the send call is an array of information about the sending of your message. To check the send status later on, you just need to call the messageStatus method:

// Get the message send status by received ID
$result = $router->messageStatus(array(19896128));

// result:  Array ( [19896128] => Array ( [text] => Wake up! [status] => d [created_time] => 1325261093 [reply_number] => 447624800500 [completed_time] => 1325261120 [credits_cost] => 1 ) )
If you want to check a phone number's status and the credit cost of sending a text message to that number, you can make a call to checkNumber:

// Get the message number information
$result = $router->checkNumber(array(9991234567));

// result:  Array ( [16083359316] => Array ( [price] => 1 [country] => US ) )
TextMagic is a really awesome service. The account is easy to create, the API is simple, and best of all, no need to know the recipient's carrier! The one downside I've found is that I cannot control the "From" name or phone number, so the the text comes from a static number. Beyond that, TextMagic is outstanding!

Update: you may change the "from" address within your TextMagic account. The feature does not work in the United States or Mexico, however.

0 comments:

Post a Comment