MessageMedia is a company that helps delivering text messages (SMS) over the internet using front-end applications in connection with an API
The sending of text messages from CodeIgniter happens over a CURL connection to the messageMedia API.
I will explain how to build the class that will help us doing this in a bit but have a read through the pages above as they will help you a lot to understand.
First though prepare yourself and think of the following aspects you might want to integrate as well:
- Storing some of the information in your database, maybe to display the content in the users’ private area,
- Making sure that the destination phone numbers are written in international format (leading + sign, no spaces)
You will see the sanitizing method inside the class. - Add the opportunity for testing without SMS being sent (I will give an example)
But let’s get started with the library.
The example that you will see below is taken from a booking project that I was working on. Some of the elements are related to it but easy to recognize and modify into your needs.
Under application/libraries add a new folder: MessageMedia
There under, add your file MessageMedia.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); /** * class MessageMedia * * Build a connection to the messageMedia API to send SMS messages given the mobile number * Also, save the result into the datbase table 'tb_message_media_log' * * Jerome Manceau - 2017 * * USAGE: * $this->load->library('messageMedia/MessageMedia'); * $this->messagemedia->set_testing(MESSAGE_MEDIA_TEST); // this constant is set to true or false in the constants.php (see also php logs) * $this->messagemedia->set_mobile(<the-mobile-number>); // format: +6143434323433 * $this->messagemedia->set_msg_type( 'BOOKING' ); // or one of the other types (see method description) * $this->messagemedia->set_id_from( <user id that triggered SMS> ); * $this->messagemedia->set_id_to( <user id of receiving SMS> ); * $this->messagemedia->set_text( SMS_BOOKING_MADE ); // see constants.php (specific text sent for booking) * $this->messagemedia->send_sms_message(); */ class MessageMedia { public $mobile; public $message; public $testing = false; public $testlog; private $url; private $cred; private $info_code; private $msg_id; private $status; private $tbl_view_msg_id = 0; private $msg_type; private $id_from = 0; private $id_to = 0; protected $CI; public function __construct() { $this->CI =& get_instance(); $this->url = MESSAGE_MEDIA_URL; $this->cred = base64_encode(MESSAGE_MEDIA_CRED); } /* * Testing Mode */ public function set_testing( $test=false ) { $this->testing = $test; } /* * In case of Testing Mode, you can view the output * after having sent the message */ public function get_testing_output() { return $this->testlog; } /* * In case of Testing Mode, you can get the output as Array * after having sent the message */ public function get_testing_array() { return $this->testlogArray; } public function set_mobile( $num=null ) { if ($num == MESSAGE_MEDIA_JEROME_FAKE) { $num = MESSAGE_MEDIA_JEROME_REAL; } //* the following is to make sure that no leading 0 has been forgotten //* a number should look like this +61423223342, not +610423223342 if (strpos($num, '0') == 3) { $num = preg_replace("/(^.{3})0/", "$1", $num); } $this->mobile = $num; } /* * set text for SMS * see constants.php for available texts (eg. SMS_BOOKING_MADE) * The text that we are sending to the customer is not a full message * from someone else, but a notice that something has been performed... */ public function set_text( $text=null ) { $this->message = $text; } /* * The message type defines why SMS was triggered * and has to be set in the controller * So far there is a choice of: * - BOOKING -> a booking has been made * - BOOK DECL -> a booking has been declined * - BOOK ACCPT -> a booking has been accepted * --> This is optional and only as information inside your application (here the appication was about bookings) */ public function set_msg_type( $type=null ) { $this->msg_type = $type; } /* * id of user who triggered the sending of SMS * this id is 0 by default and is stored like this * when message is sent by application */ public function set_id_from( $id=null ) { $this->id_from = $id; } /* * set the id of user who is receiving SMS */ public function set_id_to( $id=null ) { $this->id_to = $id; } /* * Store information into database */ private function store_db_log() { $data['user_id_from'] = $this->id_from; $data['user_id_to'] = $this->id_to; $data['destination'] = $this->mobile; $data['message_id'] = $this->msg_id; $data['status'] = $this->status; $data['info_code'] = $this->info_code; $data['msg_type'] = $this->msg_type; return $this->CI->db->insert('message_media_log', $data); // you will have to create this table first } private function check_mobile_format() { $ret = ''; if (!empty($this->mobile)) { $ret = strip_tags($this->mobile); $ret = preg_replace("/\s+/",'', $mobnum); // remove all white spaces $ret = preg_replace("/\(0{1,2}/",'', $ret); // remove opening brackets with 1 or 2 zeros $ret = preg_replace("/\(/",'', $ret); // remove other opening brackets $ret = preg_replace("/\)/",'', $ret); // remove closing brackets $ret = preg_replace("/^0+/",'', $ret); // remove leading zeros if ( !preg_match("/\+/", $ret) ) { $ret = '+' . $ret; } } return (preg_match("/^\+\d{6,18}$/", $ret)) ? true : false; // generous check at the end for validity } public function send_sms_message() { $ch = curl_init($this->url); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Accept: application/json', 'Content-Type: application/json', 'Authorization: Basic '.$this->cred )); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $data = array( 'messages' => array( array( 'content'=>$this->message, 'destination_number'=>$this->mobile, 'format'=>'SMS' ) ) ); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); if ( $this->testing ) { $this->testlogArray = $data; ob_start(); print_r($data); $this->testlog = ob_get_contents(); ob_end_clean(); error_log("=== TESTING SMS: \n" . $this->testlog); } else { if ($this->check_mobile_format()) { $response = curl_exec($ch); $r = json_decode($response); $this->info_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $this->msg_id = $r->messages[0]->message_id; $this->status = $r->messages[0]->status; } else { // normally set by messageMedia, in this case by us: $this->status = 'WRONG NUMBER FORMAT'; } $log_id = $this->store_db_log(); } } }
In your controller, there were the message will have to be sent, you will have something like this:
if ( $sendSMS && $sms_info['mobile_num'] ) { $this->load->library('messageMedia/MessageMedia'); $this->messagemedia->set_testing(MESSAGE_MEDIA_TEST); // true or false depending on your settings in constants.php $this->messagemedia->set_mobile($sms_info['mobile_num']); $this->messagemedia->set_msg_type( 'BOOK ACCPT' ); $this->messagemedia->set_id_from( $id_from ); $this->messagemedia->set_id_to( $id_to ); $this->messagemedia->set_text( SMS_BOOKING_ACCEPTED ); $this->messagemedia->send_sms_message(); $sms_testing_array = $this->messagemedia->get_testing_array(); }
Leave a Reply