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
    JavaScript Promise API

    While synchronous code is easier to follow and debug, async is generally better for performance and flexibility. Why "hold up the show" when you can trigger numerous requests at once and then handle them when each is ready?  Promises are becoming a big part of the JavaScript world...

  • By
    Create a Sheen Logo Effect with CSS

    I was inspired when I first saw Addy Osmani's original ShineTime blog post.  The hover sheen effect is simple but awesome.  When I started my blog redesign, I really wanted to use a sheen effect with my logo.  Using two HTML elements and...

Incredible Demos

  • By
    jQuery topLink Plugin

    Last week I released a snippet of code for MooTools that allowed you to fade in and out a "to the top" link on any page. Here's how to implement that functionality using jQuery. The XHTML A simple link. The CSS A little CSS for position and style. The jQuery...

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

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!