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.

Track.js Error Reporting

Upcoming Events

Recent Features

Incredible Demos

  • External Site Link Favorite Icons Using MooTools and CSS

    I recently came upon an interesting jQuery article about how you can retrieve all external links within a page, build the address of the site's favorite icon, and place the favorite icon along side the link. I've chosen a different approach which...

  • Comment Preview Using MooTools

    Comment previewing is an awesome addition to any blog. I've seen really simple comment previewing and some really complex comment previewing. The following is a tutorial on creating very basic comment previewing using MooTools. The XHTML You can set up your XHTML any way you'd like....

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!

Recently on David Walsh Blog

  • Intercept HTTP Requests with Node.js nock

    Unit testing external APIs is difficult no matter what language you do it in.  Hell, working with any external API is scary, if only because you have zero control of networking issues, API changes, and a host of other issues.  But if you do create a service...

  • Introducing Frontend Masters (with Giveaway)

    Hey DWB readers, I'm super happy to sponsor this blog. I've been a long-time reader and fan, since back when David wrote about JavaScript and MooTools back in 2007. ;-) We are in one of the fastest changing, evolving, most lively communities on earth: JavaScript and front-end web...

  • Get Node.js Command Line Arguments with yargs

    Using command line arguments within Node.js apps is par for the course, especially when you're like me and you use JavaScript to code tasks (instead of bash scripts).  Node.js provides process.argv but that doesn't provide a key: value object like you'd expect: Bleh.  If you want to work with a...

  • OâReilly Velocity Conference â New York

    My favorite front-end conference has always been O'Reilly's Velocity Conference because the conference series has focused on one of the most undervalued parts of client side coding:  speed.  So often we're so excited that our JavaScript works that we forget that speed, efficiency, and performance are just as important. The next Velocity...

  • Free Download: Font Bundle Featuring 17 Incredible Typefaces

    The only thing we love more than a good font, is a good free font. So we’ve combed the Web for some of our favorite free fonts, and gathered them here in a single download. You’ll find a variety of useful typefaces, from highly geometric designs...