TUTORIAL

Send SMS with PHP - 3 Simple Steps to Get Started with 46elks

If you know how to program in PHP you can send text message with 46elks. Just follow these three simple steps to get started and in a few minutes you will receive an SMS that you have sent from PHP.

🇸🇪 Är du i Sverige så rekommenderar vi att du läser den svenska versionen av denna guide.

1. Create a user

To send an SMS with 46elks you first have to create an account. Go to the registration page and fill in your details. You will then receive an automatic phone call to verify your new account.

To start using 46elks to send text messages you will need to purchase some credits. The minimum purchase is 10 EUR which is more than enough to get started.

2. Username and password

Once you have an account it’s time to login to the 46elks dashboard. This is where you can see all your important account information - like the username and password you need to send an SMS. The username is called API username and the password API password.

Tip: Click on the blurred text under API password to see your own password.

Every time you do something with 46elks, like sending an SMS, you have to provide your username and password via Basic HTTP Authentication. The easiest way to do this is by including the username and password in the URL used to tell 46elks to do something. For example:

https://YOUR-USERNAME:YOUR-PASSWORD@api.46elks.com/a1/SMS

If you are using a PHP library to communicate with 46elks this library will most likely have a way to specify the Basic Authentication username and password for every request.

3. Send an SMS

Finally, it’s time to write some PHP. Your code needs to be able to do the following:

There are many ways to implement this in PHP. This is an example of how it can be done using no external libraries:

$username = 'YOUR-USERNAME'; // Specify your API username
$password = 'YOUR-PASSWORD'; // Specify your API password

$sms = array(
  'from' => 'PHP',   // The sender of the SMS
  'to' => 'YOUR-PHONE-NUMBER',  // The number that will receive the text message
  'message' => 'It is easy to send SMS with 46elks', // The content of the text message
);

$context = stream_context_create(array(
  'http' => array(
    'method' => 'POST',
    'header'  => "Content-type: application/x-www-form-urlencoded",
    'content' => http_build_query($sms),
)));

$response = file_get_contents("https://$username:$password@api.46elks.com/a1/SMS", false, $context);

if (!strstr($http_response_header[0],"200 OK")){
  echo $http_response_header[0];
}
else {
  echo "$response \n";
}

Tip: The phone number to send to should be formatted with country code and without spaces, dashes or parentheses. For example +468123123 and not 08-123123 or +46 (8) 123 123. The sender of the SMS set in from can be up to 11 characters long, starting with a letter and contain only letters and numbers. For example The46elks and not 46elks.

Use the PHP example above to send yourself an SMS and your phone will buzz. Congratulations, you’ve just sent an SMS from PHP.

Next step

You’ve just sent a text message with a text sender (i.e. not from a phone number). By doing this you can use SMS to remind customers of reservations, send confirmations of new orders, etc.

To send an SMS is the easiest way to get started with 46elks, but you can do so much more. For example you can receive SMS, automatically respond to an incoming SMS or handle voice calls. For this and more there are a load of PHP code examples at GitHub.

If you have any questions then get in touch, we love talking to our users!