SMS from web application

PhpSms

Php Problem Overview


I just want to send SMS from my web application in PHP. Can anyone tell me how to do this? What all things I need to do for this?

Php Solutions


Solution 1 - Php

I don't know if this applies to you, but what I have done many times to save myself the money is ask the user in his profile what his carrier is, then tried matching it with this list. Essentially, many/most carriers have an email address connected to a phone number that will easily let you send texts to the number. For example, if you have ATT and your phone number is 786-262-8344, an email to [email protected] will send you a text message with the subject/body of the email, free of charge. This technique will pretty much cover all of your US users for free. Obviously, depending on the needs of your application this may not be possible/adequate/desired, but it is an option to be aware of.

Solution 2 - Php

Your main option for sending SMS messages is using an existing SMS provider. In my experience (which is extensive with SMS messaging web applications), you will often find that negotiating with different providers is the best way to get the best deal for your application.

Different providers often offer different services, and different features. My favourite provider, and indeed, the one that has happily negotiated with me for lower rates in the past, is TM4B (http://www.tm4b.com). These guys have excellent rates, cover a huge proportion of the globe, and have excellent customer service.

Below is some code extracted (and some parts obfuscated) from one of my live web applications, for sending a simple message via their API:

require_once("tm4b.lib.php");
$smsEngine = new tm4b();

// Prepare the array for sending
$smsRequest["username"] = "YOURUNAME";
$smsRequest["password"] = "YOURPWORD";
$smsRequest["to"] = "+441234554443";
$smsRequest["from"] = "ME!";
$smsRequest["msg"] = "Hello, test message!";

// Do the actual sending
$smsResult = $smsEngine->ClientAPI($smsRequest);

// Check the result
if( $smsResult['status'] == "ok" ) {
    print "Message sent!";
} else {
    print "Message not sent.";
}

Many other providers that I've used in the past, have very similar interfaces, and all are really competitive when it comes to pricing. You simply have to look around for a provider that suits your needs.

In regard to cost, you're looking at prices ranging from a few pence/cents for most Western countries (prices are a little bit higher for most third-world countries, though, so beware). Most providers you will have to pay in bulk, if you want decent rates from them, but they'll often negotiate with you for 'smaller-than-usual' batches. Most providers do offer a post-pay option, but only when you've successfully completed a few transactions with them... others offer it from the start, but the prices are extortionate.

Solution 3 - Php

Send only, but cheap and easy

The simplest way is definitely using the email hack that @Paolo Bergantino mentioned above. It's easy to ask your users to type in their phone number and select their carrier from a list. It's also easy, on absolutely any development platform, to send email once you have the user's info.

There are two very important limitations that I've discovered with that approach, however:

  1. The first is that the cellular carriers all prioritize SMS messages sent through their email gateways below other SMS traffic. Probably because they aren't getting paid by the sender for these. Don't use this method if minimizing latency is important to you.
  2. The second is that especially in this modern era of portable phone numbers, users will switch carriers from time to time, and will almost certainly forget to tell you. If you need to be able to reliably deliver SMS messages to the same people a year or two from now, this method will begin to fail.

Send and receive for cheap

If you're trying to set your site up to send and receive SMS messages on a budget, you can use a service such as TextMarks. TextMarks lets you pick a keyword for your service that allows users to route messages to you through TextMarks' shared short code, 41411. The catch here is that they reserve 20 characters in each message for short advertisements to pay for their services.

Professional quality

If you require low latency, high reliability, and no advertising, you'll pretty much have to go through an SMS aggregator unless you're big enough to negotiate with each carrier individually.

The biggest thing I've found to watch out for with SMS aggregators is that many of them are really set up to be marketing companies, not application hosts--they don't really understand using SMS as a channel through which users can interact with service providers. I've found that the aggregators that use the phrase "your campaign" rather than "your application" are the worst offenders. Stay away from them, and find an aggregator that understands your needs as an application developer.

Solution 4 - Php

Twilio recently released an API to send SMS messages. The api is rather simple, basic REST interface to send SMS, and a post returning document to respond.

Solution 5 - Php

Hi my name is Jarod and I work for Twilio.com so I am a little biased. But with that said, it is incredibly easy to send an SMS from your PHP web application using the Twilio REST API. Here is a simple example:

<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('/path/to/twilio-php/Services/Twilio.php'); // Loads the library
 
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "{{ account_sid }}"; 
$token = "{{ auth_token }}"; 
$client = new Services_Twilio($sid, $token);
 
$client->account->messages->sendMessage("+14158141829", "+14159352345", "Hi Shyju, Sorry it took me so long to get back to you. <3 The Internet.");

Solution 6 - Php

We use a commercial solution called TextAnywhere for this.

For a small fee (a few pence per SMS), they give you a Web service (plus APIs for .NET, ASP, Java, C++, etc.). It's easy and it works, although it's not free.

At its most basic, it's as simple as making a URL request with the number and the message text in the QueryString, and their server does the rest for you.

Solution 7 - Php

If you know the carrier, you are golden. Most have email gateways. If not you can use Clickatell API which costs about $0.04 per message. I am looking into a hack now where you would use a GSM modem and your individual cell phone plan with unlimited SMS. This could save a lot of money if it works.

Solution 8 - Php

If you are willing to invest in a android smart phone you can set up your own SMS gateway that comes out cheaper in the end.

I use SMS GATEWAY ME which is a free android app you can send a message like this:

$URL = "http://v2.smsgateway.me/API/Send/Single.php";

$postdata = http_build_query(
    array(
	'Username' => "[email protected]",
	'Password' => "password",
	'Number' => "+447791064782",
	'Message' => "Hello World!",
    )
);

$opts = array('http' => array('method'  => 'POST', 'header'  => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata));

$context  = stream_context_create($opts);

$result = file_get_contents($URL, false, $context);

There is a documentation on their site that tells you how to accept incoming messages as well.

Solution 9 - Php

If you need a reliable way to send many SMS, you should look for a SMS service provider that acts as a web to SMS gateway. In that case there will be a http based interface, allowing you to send SMS by calling a URL and passing recipient and message as parameters.

Solution 10 - Php

dktext not only allows you to send bult text messages to requesting customers but the will soon be providing google voice integration to use the same system with your google voice account

Solution 11 - Php

To send SMS from a web application you need to connect your application to SMS Gateway. Most SMS gateways provide external API which can be used to do that. They usually have documentation which you can use to find out how to integrate SMS notification in your PHP code. Unfortunately there are no reliable free sms service provides so you have to pay for this sms service. You can check http://www.phpjabbers.com/web-sms/ which seems to work a lot easy. Their integration code is pretty simple:

<?php
$SMSLIB["phone"] = '44111222333'; // your phone number
$SMSLIB["key"] = 'abcdefghijklmnopqrs'; // your personal API code
include("smslib.php"); // we will provide you with smslib.php file
sendSMS("test message", $SMSLIB["phone"]); // function which sends the message
?>

Solution 12 - Php

I had to implement user-initated sms functionality on our website recently and found the www.dotgo.com service helpful. It's free... looks like it was created by a couple of PhD's as a sort of stateless messaging framework of sorts (think http request-response model).

To get it working you set up an "index.crml" file (similar to index.html, php, etc). Ours looks like this (sorry all putting it all on one one line... having some issues getting it to display otherwise):

<?xml version="1.0" encoding="UTF-8"?><cmrl xmlns:dotgo="http://dotgo.com/cmrl/1.0"><match pattern="*"><engine href="http://www.bulbstorm.com/sms/flashbulb.php"/></match></cmrl>

On our site, the index file in turn references the /sms/flashbulb.php file, which (excluding opening and closing php tags) looks like this:

$wordArray = explode(' ',$_REQUEST['sys_argument']);
$username = strip_tags($wordArray[0]);
$messageBody = str_replace($username.' ', '', $_REQUEST['sys_argument']);
require_once 'Database.php';
$dbh = new Database('bulbstorm');
$args = array($username, $messageBody);
$dbh->execMysqlProc('uspAddFlashbulb', $args);
print "<message><content>Bulb received and saved to your account</content></message>";

Anyway, I only include the code to give some sense of how the framework functions and how little code there is to write to get something functional.

There are some limitations. The foremost being that everything is user-initated. So if you're primarily looking to send outgoing messages that are not preceded by your user sending a message to your site to "get" the response message then it's probably not what you want. Worked for what we were doing though. One of the founders even personally responded to an emailed question and was very helpful.

One of the features that we haven't used yet, but have considered is their subscription functionality... where users can set it up so that the dotgo system periodically polls a page on your site, sending an sms message to their phone per the schedule the user specifies. Again, I didn't take it that far, but thought it was interesting.

Solution 13 - Php

I think, it is the oldest question by this theme. Mainly answers - advertizing of paid gateways. But there is one more way - sms from android device (sim card), android gateway. I created a free open source application with simple API: https://github.com/pavelakafoks/SmsTransmitter Google play: https://play.google.com/store/apps/details?id=info.ininfo.smstransmitter You can use my application for your site or build your own android application based on my sources. It is more cheaper than internet sms gateways. I hope it will be useful for somebody.

Solution 14 - Php

Basically, each carrier has a email gateway to which you will need to send an email. You will need to know the carrier in advance so that you know which email address to send the SMS message.

Solution 15 - Php

function sendSMS($number,$message) {
	$request = message api link;
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $request);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$response = curl_exec($ch);
	curl_close($ch);
	return split(',',$response);
}

Solution 16 - Php

It's a good idea to use multigateway solution. I've created one for my purposes, but I think it could be useful for anyone who want to easy set up: https://packagist.org/packages/orajo/sms-zilla

$smsSender = new SmsZilla\SmsSender(new SmsZilla\Adapter\MockAdapter());

$smsSender->setRecipient('605123456');
// adding more recipients (with and without country code)
$smsSender->setRecipient(['511654321', '48511654987', '+41751654987']);
  
$smsSender->setText("Message text");
$result = $smsSender->send();

Right now this library support 5 providers, 6th is in dev and others are planned.

Solution 17 - Php

You can use our API and this tutorial which covers the topic.

But, basically, sending single sms is as simple as sending HTTP POST request:

curl -X "POST" https://api.wavecell.com/sms/v1/amazing_hq/single \
    -u amazing:1234512345 \
    -H "Content-Type: application/json" \
    -d $'{ "source": "AmazingDev", "destination": "+6512345678", "text": "Hello, World!" }'

Where amazing is your account, 1234512345 - the password (use stronger passwords!) and amazing_hq is your subaccount.

Which results in: SMS

Details about sending HTTP request in PHP are covered in the following questions:

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShyjuView Question on Stackoverflow
Solution 1 - PhpPaolo BergantinoView Answer on Stackoverflow
Solution 2 - PhpJames BView Answer on Stackoverflow
Solution 3 - PhpsblomView Answer on Stackoverflow
Solution 4 - PhpTim LytleView Answer on Stackoverflow
Solution 5 - PhpJarod ReyesView Answer on Stackoverflow
Solution 6 - PhpDylan BeattieView Answer on Stackoverflow
Solution 7 - PhpTonyView Answer on Stackoverflow
Solution 8 - Phpuser3144832View Answer on Stackoverflow
Solution 9 - PhplcvinnyView Answer on Stackoverflow
Solution 10 - PhpMichaelView Answer on Stackoverflow
Solution 11 - PhpgeorgeView Answer on Stackoverflow
Solution 12 - PhpcodemonkeyView Answer on Stackoverflow
Solution 13 - PhpPavel SamoylenkoView Answer on Stackoverflow
Solution 14 - PhpArielView Answer on Stackoverflow
Solution 15 - Phpshiyani sureshView Answer on Stackoverflow
Solution 16 - PhpJarosław WasilewskiView Answer on Stackoverflow
Solution 17 - PhpastefView Answer on Stackoverflow