How to use nexmo sms gateway in cakephp


Posted on 09 Mar 16 by Vickey

SMS gateway is used to send text messages from web page to mobile. Many SMS Gateway company provide this functionality by their apis. By using these apis we can send sms from our web pages on a single click. We use this service for send otp and notify our users. There are many SMS Gateway in the market. One of them is nexmo. For using the nexmo apis we need to create an account on nexmo and it provide us api_key and api_secret which used in apis for Authentication. Before start  you need to add and verify your mobile number and a test mobile number. you can add your test mobile number by using this url

Here i am using nexmo api with cakephp

First of all we need to download nexmo api SDK from its website. After download  put it in to  'app/vendor' directory and import it in 'AppController'

App::import('Vendor', 'Nexmo', array('file' => 'Nexmo/NexmoMessage.php'));

Now write a function in AppController

protected function _nexmoSendMessage($from = null, $to = null, $text = null)
	try {
		$url = '';
		$queryParam = array(
			'api_key' => 'api_key',
			'api_secret' => 'api_secret',
			'from' => $from,
			'to' => $to,
			'text' => $text
		$urlQueryString = $url . http_build_query($queryParam);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $urlQueryString);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Receive server response
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		$responseData = curl_exec($ch);
		if (curl_error($ch)) {
			throw new Exception(curl_error($ch));
		curl_close ($ch);
		$data = json_decode($responseData, true);
		if (!isset($data['messages'])) {
			throw new Exception('Unknown API Response.');
		foreach ($data['messages'] as $message) {
			if ($message['status'] != 0) {
				throw new Exception($message['error-text']);
	} catch(Exception $e) {
		CakeLog::write('nexmo_sms_gateway_error_log', $e->getMessage());

add your api_key and api_secret

Now use this function in your app where you want send message

public function send(){

	$this->_nexmoSendMessage($from, $to, $text);


You can download source code here

If this post help you reduce time to develop, you can help me to maintain this website :)