Tutorials

SENDING SMS

				
					<?php 

// Assuming you have the necessary credentials to connect to your MySQL database 
$servername = "xxxxxx"; 
$username = "xxxxxxx"; 
$password = "******"; 
$dbname = "xxxxxxx"; 
// Create a connection to the database 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check the connection 
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error); 
} 
// Read the raw data from the incoming HTTP request's body 
$data = file_get_contents("php://input"); 
// Decode the JSON data into an associative array 
$decoded_data = json_decode($data, true); 
//put data to the msgdata.txt 
file_put_contents('msgdata.txt', $data, FILE_APPEND); 
// Check if JSON decoding was successful 
if ($decoded_data === null) { 
    echo "Error decoding JSON data."; 
    exit; 
} 
// Extract data from the new JSON structure 
$number = $decoded_data['number']; 
$message_code = $decoded_data['MsgFollowUpUniqueCode']; 
$status = $decoded_data['Status']; 
// Escape each value to prevent SQL injection 
$number = $conn->real_escape_string($number); 
$message_code = $conn->real_escape_string($message_code); 
$status = $conn->real_escape_string($status); 

// SQL query to insert the data into a table named 'egosmsdata' 
$sql = "INSERT INTO egosmsdb (number, message_code, status) VALUES ('$number', '$message_code', '$status')"; 
if ($conn->query($sql) !== TRUE) { 
    echo "Error in saving delivery report: " . $conn->error; 
    $conn->close(); 
    exit; 
} 
echo "delivery report saved successfully into the database."; 

// Close the database connection 
$conn->close(); 
?> 
				
			

SENDING SMS

Using JSON

Overview

This method provides a way to send the message (SMS) via JSON using account details.

Send SMS through your application by making an HTTP POST request to the following endpoints:

JSON Request Parameters

These are the standard request parameters that are needed when using the EgoSms JSON Api

userdataString Required

Contains the user information.

msgdataString Required

Contains the message information.

usernameString Required

The account user name.

passwordString Required

The account password.

numberString Required

The phone number the message is being sent to.

messageString Required

This represents the message you are sending out. The message source must have a maximum of 160 characters.

senderidString Required

This represents who the message is coming from. Sender can have a maximum of 11 characters.

Request format

Request for sending one message.

				
					{
   "method":"SendSms",
   "userdata":{
      "username":"xxxxxx",
      "password":"xxxxxx"
   },
   "msgdata":[
      {
         "number":"2567xxxxxxxx",
         "message":"xxxxxx",
         "senderid":"xxxxxx"
      }
   ]
}
				
			

Request for sending multiple messages.

				
					{
   "method":"SendSms",
   "userdata":{
      "username":"xxxxxxxx",
      "password":"xxxxxxxx"
   },
   "msgdata":[
      {
         "number":"2567xxxxxxxxx",
         "message":"First Message",
         "senderid":"Egosms"
      },
      {
         "number":"2567xxxxxxxxx",
         "message":"Second Message",
         "senderid":"Egosms"
      },
      {
         "number":"2567xxxxxxxxx",
         "message":"Third Message",
         "senderid":"Egosms"
      },
      {
         "number":"2567xxxxxxxxx",
         "message":"Fourth message",
         "senderid":"Egosms"
      }
   ]
}
				
			

System Feedback

For a successful request

Once your message has been successfully submitted to the Egosms system, it will return Json in the format shown below.
				
					{
   "Status":"OK",
   "Cost":"xxxx",
   "MsgFollowUpUniqueCode":"xxxxx"
}
				
			

For a failed request

Once the request fails, Json in the  format below will be returned.
				
					{
   "Status":"Failed",
   "Message":"xxxxxx"
}
				
			

Example

Submited JSON

				
					{
   "method":"SendSms",
   "userdata":{
      "username":"Egotest",
      "password":"xxxxxxxx"
   },
   "msgdata":[
      {
         "number":"256707811113",
         "message":"Welcome to Egosms",
         "senderid":"Egosms"
      }
   ]
}
				
			

Returned JSON for a Successful Request

				
					{
   "Status":"OK",
   "Cost":"35",
   "MsgFollowUpUniqueCode":"ApiJsonSubmit53b5155c1a3f90.65989312"
}
				
			

Returned JSON for a Failed Request

				
					{
   "Status":"Failed",
   "Message":" Error Response message"
}
				
			

Possible Error Messages

Wrong JSON Passed

Occurs if there is a syntax error in your json code.

Money Not Enough To Send This Batch of Msgs

Occurs if there are insufficient funds on the sender’s account.

Wrong Username or Password

Occurs if either the password or username is wrong.

That user does not exist or user not active

Occurs if the user account does not exist or is inactive.

Either Username or Password Not Set

Occurs if either the username or password is empty.

Programming Example

				
					<?php
$data = array(
    'method' => 'SendSms',
    'userdata' => array(
        'username' => 'Egotest', // Egosms Username
        'password' => 'xxxxxxx'
        //Egosms Password    ),
        'msgdata' => array(
            array(
                'number' => 256707811113,
                'message' => 'elias',
                'senderid' => 'Good'
            ) ,
            array(
                'number' => 256788200915,
                'message' => 'marvin',
                'senderid' => 'Bad'
            )
        )
    );

    //encode the array into json
    $json_builder = json_encode($data);
    //use curl to post the the json encoded information
    $ch = curl_init('https://www.egosms.co/api/v1/json/');

    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json'
    ));
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_builder);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $ch_result = curl_exec($ch);
    curl_close($ch);
    //print an array that is json decoded
    print_r(json_decode($ch_result, true));
?>

				
			

Tutorials

SENDING SMS

				
					<?php 

// Assuming you have the necessary credentials to connect to your MySQL database 
$servername = "xxxxxx"; 
$username = "xxxxxxx"; 
$password = "******"; 
$dbname = "xxxxxxx"; 
// Create a connection to the database 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check the connection 
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error); 
} 
// Read the raw data from the incoming HTTP request's body 
$data = file_get_contents("php://input"); 
// Decode the JSON data into an associative array 
$decoded_data = json_decode($data, true); 
//put data to the msgdata.txt 
file_put_contents('msgdata.txt', $data, FILE_APPEND); 
// Check if JSON decoding was successful 
if ($decoded_data === null) { 
    echo "Error decoding JSON data."; 
    exit; 
} 
// Extract data from the new JSON structure 
$number = $decoded_data['number']; 
$message_code = $decoded_data['MsgFollowUpUniqueCode']; 
$status = $decoded_data['Status']; 
// Escape each value to prevent SQL injection 
$number = $conn->real_escape_string($number); 
$message_code = $conn->real_escape_string($message_code); 
$status = $conn->real_escape_string($status); 

// SQL query to insert the data into a table named 'egosmsdata' 
$sql = "INSERT INTO egosmsdb (number, message_code, status) VALUES ('$number', '$message_code', '$status')"; 
if ($conn->query($sql) !== TRUE) { 
    echo "Error in saving delivery report: " . $conn->error; 
    $conn->close(); 
    exit; 
} 
echo "delivery report saved successfully into the database."; 

// Close the database connection 
$conn->close(); 
?>