Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Send SMS Campaigns and Get Status Updates With PHP and Routee

DZone's Guide to

Send SMS Campaigns and Get Status Updates With PHP and Routee

Learn how you can boost user engagement by sending SMS messages to multiple recipients with the Routee platform and Contacts API.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

Send Bulk Messaging 

Building consumer awareness and engagement is easy with a bulk SMS API, which gives you the ability to deliver the same message to many recipients and design compelling marketing campaigns. 

The Routee API will enable you to: 

  • Put your messaging load on Routee and stay focused on your application’s functionality.
  • Customise your message by using Routee's advanced Contacts API
  • Send your messages to multiple recipients. 
  • Personalise your message content using custom labels.  
  • Respect recipients' privacy and legal restrictions per country, by using the "Quiet Hours” feature. 
  • Get insights on every message so to be sure that SMS were delivered on time, 
  • View reports, graphs, and calculate the cost of your campaigns. 
  • Send large texts (more than 160 characters), written in any language.

Let’s see how you can send a personalized message from your web application to all your customers.

Initially, you should import all your contacts into the Routee platform. 

Note:

  1. You can assign custom information to any contact and then use them as custom labels to the message bodies.

  2. You can use as many custom labels as you wish.

  3. Fallback values can be set for every custom label, in case the field is empty.

For instance, the following example states that custom label [firstName] will be replaced with [user] if a contact does not have the firstName label set. 

A Contact’s Default Name Value 

We assume that you have imported your contacts into the Routee platform and grouped them under the name “users.”

Now, your application should make a Routee API call to https://connect.routee.net/sms/campaign.

HTTP Request Headers

KEY VALUE
Content-Type Application/json
Authorization Bearer {access_token}

 Code sample: 

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://connect.routee.net/sms/campaign",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"body\": \"Hello, a new version of MindPuzzle is available. Check it out\", \"to\" : [ \"+30697xxxxxxx\", \"+30694xxxxxx\", \"+30963xxxxxxx\" ],\"from\": \"mindpuzzle\", \"campaignCallback\": { \"strategy\": \"OnCompletion\",\"url\": \"http://xxx.xxxxxxxx.xxx/campaign\" }, \"callback\": {\"strategy\": \"OnChange\", \"url\": \"http://xxx.xxxxxxxx.xxx/message\"}}",
  CURLOPT_HTTPHEADER => array(
    "authorization: Bearer 12dc9ff4-7df4-4786-8e7a-a46d317687f4",
    "content-type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Using the expression [~firstName], we set Routee to replace that string with the “users” group firstName property.

The sent message is a message with sender ID “mindgames” and was sent to all contacts inside “users” group, using a custom label which contains their first name.

The message body would look like this:

"Hello Joe, a new escape room has been added to MindGames. Check it out today!"

Get Status Updates

For each Routee application you can enable a Callback, to receive API responses for single or bulk messages (campaigns).

Note:
You can check the default API calls’ values here.

 In the following example, we will schedule a Bulk SMS from “MindGames” application towards a contact list while getting campaign’s status updates in a predefined Callback URL.

{    
   "campaignCallback":{       
        "strategy":"OnCompletion",           
        "url":"http://www.exampleserver.com/mindgames/campaigns"    
    },   
   "callback": {     
      "strategy": "OnCompletion",      
      "url": "http://www.exampleserver.com/message"   
   },    
  "body":"Welcome to MindGames! Invite a friend and special gifts are waiting for you both. Learn more at http://my.link/url",  
   "campaignName":"Welcoming",  
   "to":[       
        "+30694xxxxxxxx",       
        "+35594xxxxxxxx",       
        "+44794xxxxxxxx",       
        "+30694xxxxxxxx",       
        "+22894xxxxxxxx",       
        "+32294xxxxxxxx"        
     ],  
   "scheduledDate":"2017-08-20T09:00:01Z",  
   "from":"MindGames" 
}

The message is scheduled to be sent on August 20, 2017, at 09:00:01 UTC towards 6 recipients from 5 different countries.

Its body will look like:

"Welcome to MindGames! Invite a friend and special gifts are waiting for you both. Learn more at http://my.link/url"

 For the MindGames app, two callback URLs have been set to receive the status updates. The first will receive the final status of the campaign, and the second, the final status for each recipient.

1st: http://www.exampleserver.com/mindgames/campaigns

2nd: http://www.yourserver.com/message 

For instance: 

<?php  

$curl = curl_init();  

curl_setopt_array($curl, array(   
   CURLOPT_URL => "https://connect.routee.net/sms/campaign",    
   CURLOPT_RETURNTRANSFER => true,   
   CURLOPT_ENCODING => "",   
   CURLOPT_MAXREDIRS => 10,   
   CURLOPT_TIMEOUT => 30,   
   CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,   
   CURLOPT_CUSTOMREQUEST => "POST",   
   CURLOPT_POSTFIELDS => "{\"campaignCallback\":{\"strategy\":\"OnCompletion\",\"url\":\"http://www.exampleserver.com/mindgames/campaigns\"}, \"callback\": {\"strategy\": \"OnCompletion\", \"url\": \"http://www.exampleserver.com/message\"}, 
\"body\":\"Welcome to MindGames! Invite a friend and special gifts are waiting for you both. Learn more at http://my.link/url\", \"campaignName\":\"Welcoming\",  
\"to\":[\"+30694xxxxxxxx\", \"+35594xxxxxxxx\",\"+44794xxxxxxxx\", \"+30694xxxxxxxx\",\"+22894xxxxxxxx\", \"+32294xxxxxxxx\" ], \"scheduledDate\":\"2017-08-20T09:00:01Z\", \"from\":\"MindGames\"}",   
   CURLOPT_HTTPHEADER => array(     
         "authorization: Bearer 12dc9fe4-7df4-4786-8d7a-a46d307687f4",     
         "content-type: application/json"   
   ), 
));  

$response = curl_exec($curl); 
$err = curl_error($curl);  

curl_close($curl);  

if ($err) {   
   echo "cURL Error #:" . $err; 
} else {   
   echo $response; 
}


Download A Buyer's Guide to Application and Data Integration, your one-stop-shop for research, checklists, and explanations for an application and data integration solution.

Topics:
api ,cloud communication ,php ,api integration ,integration

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}