Send SMS with PHP and Routee
Interested in the field of IoT? Curious about the easiest way to send SMS via your website? Sending SMS with PHP has many benefits. Discover below the steps that will help you integrate SMS functionality to any website.
How it works:
• First, you have to register for an account, if you don’t have one already.
• Get authenticated.
• Make your first call and send your first SMS
Easier than you thought, right? Now, let’s see it in action!
Register for a Routee account
Before proceeding any further, you will need to register for a Routee account.
* For further details on how to create an account head to the appendix (at the bottom of the article), “How to register for a Routee account” section.
Note: In case you wish to separate your traffic, create applications and add to them to the relevant services (optional). For further details head over to the appendix “Create an application and add services” section.
Get authenticated and make your first API call
After registering a Routee account, you will need to get authenticated with the Routee platform.
1. Head over to the Applications menu of the account panel once more.
2. Click on the lock icon next to the Application secret item.
3. Enter the password to reveal it.
Following that, you will need to encode the applicationid:applicationsecret string into a Base64 string using an encoder of your choice.
With this string, you are now able to exchange your application credentials for an access token by calling the Auth resource.
The Authorization header of the request must include the word “Basic" followed by the base64 encoded string. Each application is associated with an account.
For instance:
This string can be exchanged with an access token
POST – https://auth.routee.net/oauth/token
Request Headers:
Query Parameters:
For instance:
"https://auth.routee.net/oauth/token", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => "grant_type=client_credentials", CURLOPT_HTTPHEADER => array( "authorization: Basic NTc1NmE0MTFlNGIwNmEzM2Q1MDUxN2M3OnZiNlFwakNJT0c=", "content-type: application/x-www-form-urlencoded" ), )); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response; }
HTTP Response
{ "access_token":"string", "token_type":"string", "expires_in":"number", "scope":"string,string,string", "permissions":[ "string" ] }
Response Parameters
Thumbs up! Now you can access all Routee services using the access_token of your application to the header of each resource!
Note. All requests made for a Routee Application are associated to the Routee Account.
Important! All tokens that are issued from Routee’s authorization server are valid for 1 hour in order to secure your HTTP calls.
Once you get an HTTP response with a 401 status code, you should issue a new token and repeat your request with the new one.
To issue a new token, follow the same procedure of exchanging your application credentials through the authorization server.
SEND AN OUTGOING SMS
To send an outgoing SMS message, perform an HTTP POST request to the “https://connect.routee.net/sms".
An example request can be found right here below.
"https://connect.routee.net/sms", 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": "A new game has been posted to the MindPuzzle. Check it out","to" : "+30697ΧΧΧΧΧΧΧ","from": "amdTelecom"}", 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; }
Note: The message will be sent only to a valid phone number, written in international format e.g. +3069xxxxxxxx.
Now, let’s look at the details:
• Your header contains the proper authorization and the content type
• Your request body contains the message text (body), the sender (from) and the recipient (to)
That’s it! If your request was successful, the SMS has been successfully queued for transmission.
All clear? If not, or if you would like to learn more about our services, please feel free to contact us.
APPENDIX
How to register for a Routee account
1. Head over to the registration page.
2. Enter your details, accept the terms, and hit the Register button. Note the instructions given on each form. For instance, the password has to contain at least one number, one special character, and one capital letter.
3. An email will be sent to the address you provided. Open the link to activate your account.
You now have a Routee account! After activating it via the link provided, you will be able to navigate around the dashboard. It is recommended that new users take the tour which will quickly explain where everything is along with some of the services provided.
Create an application and add services
In order to proceed with the messaging features, you will need to create an application and add the relevant services.
1. Head over to https://dev.routee.net/#/management/applications. In case you are not already at the Applications menu, navigate there now.
2. Scroll to the bottom and click on the “Create new application” button.
3. You will be asked to enter a name and description.
Furthermore, you will need to confirm the kinds of services to include in the application:
• Lookup
• Number validation
• SMS
• Two-step authentication
• Voice messaging
*To learn more about each service and the application settings, visit https://docs.routee.net/docs/settings.