Sending Plain and Unicode SMS Messages with Python and PHP

SMS messaging is a popular way to send short text messages to mobile devices. Many businesses use SMS messaging to communicate with customers, send notifications, and more. In this tutorial, we’ll show you how to use an SMS API to send both plain and Unicode SMS messages using Python and PHP.

We’ll be using the following API endpoint to send SMS messages:


https://api.mista.io/sms

To send SMS messages, you’ll need to set up an account with the SMS provider and get an API key. Once you have your API key, you can use it to authenticate your requests to the API endpoint.

Sending Plain SMS Messages

To send a plain SMS message, you’ll need to make a POST request to the API endpoint with the following parameters:

  • to : The phone number of the recipient, including the country code. For example, +14155552671 .
  • from : The sender ID or short code that you want to use as the sender of the message.
  • unicode : A flag indicating whether the message is Unicode ( 1 ) or not ( 0 ). For plain SMS messages, set this to 0 .
  • sms : The text of the SMS message you want to send.
  • action : The action you want to take with the API. In this case, set it to "send-sms" .

Here’s an example of how you could send a plain SMS message using Python’s requests library:


import requests
url = "https://api.mista.io/sms"
headers = {"x-api-key": "your-api-key-here"}
data = {
"to": "+14155552671",
"from": "SenderID",
"unicode": "0",
"sms": "Hello, world!",
"action": "send-sms"
}
response = requests.post(url, headers=headers, data=data)
print(response.text)


And here’s an example of how you could send a plain SMS message using PHP’s curl function:


<?php 
$url = "https://api.mista.io/sms";
$headers = array("x-api-key: your-api-key-here");
$data = array(
"to" => "+14155552671",
"from" => "SenderID",
"unicode" => "0",
"sms" => "Hello, world!",
"action" => "send-sms"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;


Note that in both examples, you would need to replace the your-api-key-here value with your own API key.

Sending Unicode SMS Messages

To send a Unicode SMS message, you’ll need to make a POST request to the API endpoint with the following parameters:

  • to : The phone number of the recipient, including the country code. For example, +14155552671 .
  • from : The sender ID or short code that you want to use as the sender of the message.
  • unicode : A flag indicating whether the message is Unicode ( 1 ) or not ( 0 ). For Unicode SMS messages, set this to 1 .
  • action : The action you want to take with the API. In this case, set it to "send-sms"
  • sms : The text of the SMS message you want to send.
Joella Keza

Leave a Reply

Your email address will not be published. Required fields are marked *