PHP Text Messaging with TextMagic

By  on  

A few months back I wrote a shocking expose about how sending text messages with PHP was 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.

Recent Features

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

  • By
    Responsive and Infinitely Scalable JS Animations

    Back in late 2012 it was not easy to find open source projects using requestAnimationFrame() - this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...

Incredible Demos

  • By
    dwImageProtector Plugin for jQuery

    I've always been curious about the jQuery JavaScript library. jQuery has captured the hearts of web designers and developers everywhere and I've always wondered why. I've been told it's easy, which is probably why designers were so quick to adopt it NOT that designers...

  • By
    MooTools PulseFade Plugin

    I was recently driven to create a MooTools plugin that would take an element and fade it to a min from a max for a given number of times. Here's the result of my Moo-foolery. The MooTools JavaScript Options of the class include: min: (defaults to .5) the...

Discussion

  1. Mason

    A link to the TextMagic API/site would do wonders for improving the “usability” of this article! [/tongue-in-cheek]

  2. Somehow I’ve found Twilio to be cheaper and both more functional. They also offer a great API and messages cost only $0.01.
    I am not affiliated with them.

  3. I have seen lots of scripts which sends text messages… do i need any special server to send text message??? do it will work in all countries???

    thank you…

    • Pavel

      Using the method David described, no server is needed. Just a hosting that supports PHP.

      And it works in most countries, but not all.

  4. Alex

    There are definitely better bulk sms services than textmagic… I just can’t remember the one I used.

    • Alex

      Just found it…

      CardBoardFish SMS it was :)

    • Pavel

      This depends on the country you are aiming. Some countries are cheaper with TextMagic, some with CardBoardFish. But in the end it’s not about pricing it’s about delivery rates and support.

      Also, CardBoardFish charges for a reply number.

    • Alex

      For my country (Greece), rates from TextMagic simply suck. The 0.03 Euros from CardBoardFish is more than three times less than the TextMagic price tag per SMS.

      You can change the sender to any number (or text) you want and receive replies to those numbers. Purchasing a virtual number is not a mandatory.

      Cосать!

  5. I have to agree with Shimon. I use Twilio to send SMS at $0.01/SMS. A phone number to send the text messages from is only $1/month. And their API allows your web app to interact with messages sent back from the client/customer.

    From what I see on the TextMagic site… one SMS is anywhere from $0.10 – $0.15 per SMS depending on how many credits you buy at once. Ouch. Maybe I’m missing something?

    • Pavel

      In text messaging business US/Canada and the rest of the world are separate topics.

      Twilio is a US/Canada service. And if fact it is expensive for US, there are gateways that send messages as low as 0.7 cents per message. Twilio does not provide messaging to other countries besides US and Canada.

      TextMagic does not cover US (well kinda), but covers UK and most of the other world.

  6. Djalma Araujo

    Try to send to my cellphone in brazil. No message delivered!

  7. It’s work fine in Indonesia. thanks :)

  8. l lv to own my own website

  9. Alex

    I should mention after looking at the site, Text Magic does not support any CDMA carriers so anyone using Sprint or Verizon would be unable to receive messages from them. Pretty limiting!

  10. We’ve used http://notifynotify.com to capture SMS messages and then use that to send out promotions weekly. It’s been highly profitable for us so far. They’re built on Twilio so all carriers are good to go.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!