In this post, I will be discussing about how to send SMS through PHP using Nexmo SMS API. To send SMS, we will be using Nexmo SMS API. There are a number of SMS Gateways like Clickatell, Twilio, and so on available in the market. However, I found Nexmo to be very reliable, well Documented & cheaper.
Define Nexmo API Key:
// Nexmo api keys
define('NEXMO_API_KEY', 'XXXXXXXX');
define('NEXMO_API_SECRET', 'XXXXXXXX');
I made a global function to send sms through nexmo sms api; here is the code
/**
* Send a text message.
*
* @access protected
*
* @param string $cellNumber
* @param string $message
* @return void
*/
public function nexmoSendMessage($cellNumber = null, $message = null)
{
try {
// Step 1: Declare new NexmoMessage.
$nexmo_sms = new NexmoMessage(NEXMO_API_KEY, NEXMO_API_SECRET);
// Step 2: Use sendText( $to, $from, $message ) method to send a message.
$info = $nexmo_sms->sendText($cellNumber, 'MyApplication', $message);
// Step 3: Display an overview of the message
$nexmo_sms->displayOverview($info);
} catch(Exception $e) {
echo $e->getMessage();
}
}
Example: Lets say, you want to send an SMS to all user about the health tips.
nexmoSendMessage('+1 9876543210', 'Turn off the TV or computer, and get at least 30 minutes of exercise every day.');
0 Comment(s)