O'Reilly

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.

O'Reilly Velocity Conference
Save 20% with discount code AFF20

Recent Features

Incredible Demos

  • Create Spinning Rays with CSS3 Animations & JavaScript

    Thomas Fuchs, creator of script2 (scriptaculous' second iteration) and Zepto.js (mobile JavaScript framework), creates outstanding animated elements with JavaScript.  He's a legend in his own right, and for good reason:  his work has helped to inspire developers everywhere to drop Flash and opt...

  • Create a Dojo Lightbox with dojox.image.Lightbox

    One of the reasons I love the Dojo Toolkit is that it seems to have everything.  No scouring for a plugin from this site and then another plugin from that site to build my application.  Buried within the expansive dojox namespace of Dojo is

Discussion

  1. 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.

  2. 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.

  3. 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осать!

  4. 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.

  5. Djalma Araujo

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

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

  7. l lv to own my own website

  8. 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!

  9. 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!

Recently on David Walsh Blog

  • Open Files from Command Line on OS X

    I'm as much of a fan of application UIs as anyone else but I'm finding myself working more and more from the command line lately.  Much of that is becoming obsessed with media manipulation but I'm forcing myself to use less UIs so that I...

  • Get Stock Quotes From Command Line

    When I conned my way into my first professional programming gig, I didn't really think much about money -- just that I was getting my foot in the door.  But as my career has gone on, I've been more aware of money, investing, and retirement.  I've recently...

  • Geolocation API

    One interesting aspect of web development is geolocation; where is your user viewing your website from? You can base your language locale on that data or show certain products in your store based on the user's location. Let's examine how you can...

  • Create an Image Preview from a Video

    Visuals are everything when it comes to media.  When I'm trying to decide whether to watch a video on Netflix, it would be awesome to see a trailer of some kind, but alas that isn't available.  When I'm looking to download a video on my computer,...

  • New:  Webdesigner News!

    A new and exciting website has recently been launched for web designers and developers. You likely spend hours every morning browsing through hundreds of posts on your RSS feeds, hoping to stumble across relevant stories. Webdesigner News was built to provide web designers and developers with...