PHP Script Sends SMS Messages

You want your web server to send SMS text messages, but you don't have shell access on the machine? The Shell SMS script won't be any good for you. Fortunately there's a solution to this problem. With the aid of this PHP script your web server can still send SMS text messages, even without shell access.

For this to work the web server needs the cURL library to be installed. Most hosting providers have cURL enabled on their web servers. If not, you may make a request for the installation of the library. You can simply check if the cURL library is installed on your web server by writing the next line of code into a .php file.

<?php phpinfo(); ?>

Save this script with the name info.php for instance and put it on your web server. Then surf to your page by typing www.yoursite.com/info.php in your browser's URL line. On the page which is then shown you should be able to find the following field:

cURL enabled

You won't be able to use the SMS script unless this field is shown somewhere on the displayed page.

Sign Up With Voipbuster

Voipbuster signup

In order to be able to send an SMS text message you need a subscription with Voipbuster. After signing up there, you'll have to top up your credit. You've got plenty of options to pay them, there's probably at least one of them which will work for you. With this credit you can make phone calls and send SMS text messages almost all over the world, which is just about what we want to do. Text messages to Dutch mobile phones cost about €0.06. Messages to most other countries are also quite affordable.

Windows users can download the Voipbuster soft phone and sign up to get an account using that soft phone. Linux and Mac users can use a special link on the Voipbuster download page to sign-up using the web site only.

The PHP Script

Copy and paste the script below into your web page, or paste it in an include .php file. Make sure your web page is saved as a .php file, otherwise the code won't be executed.

It goes without saying that you'll have to change the variables $user, $pass and $from to reflect your Voipbuster account. The $from field can get the name of your Voipbuster account, or one of the verified phone numbers you have registered with your Voipbuster account. Any other value will result in your Voipbuster account name to be used as originator.

<?php

function SendSMS($to, $txt)
{
    $user = "youraccount";
    $pass = "yourpassword";
    $from = "fromyou";

    $txt = substr($txt, 0, 160);
    $url = "https://www.voipbuster.com/myaccount/sendsms.php?";
    $url .= "username=".$user."&password=".$pass;
    $url .= "&from=".$from."&to=".$to."&text=".str_replace(" ", "%20", $txt);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    curl_close($ch);
    $results = explode("\n", $result);

    return $results[4];
}

?>

The script can be called with two parameters. The first parameter is the destination phone number. This phone number has to be entered in international format, e.g.: 0031612345678. You can send SMS text messages all over the world, therefore you'll have to include the country code.
The second parameter is the text you want to send. This text is automatically truncated to 160 characters, so everything beyond that will be discarded. Trying to send more than 160 characters would result in the API failing to send the text, without even telling you about it.

The script returns a string which is succes or failed, depending on the success of the action. Below you see a piece of PHP script to give you an example how to call the SMS script.

<?php
$number = "0031612345678";
$text = "This text will be sent as message";
$result = SendSMS($number, $text);
?>

Please note that it may take up to 10 seconds for the script to complete. This delay is caused by the Voipbuster API and there's nothing we can do to accelerate it.