Posted by Arjun on Tuesday 15th May 2018

How to use Twilio to send SMS with CodeIgniter

How to CodeIgniter 3

In this post, I would like to show using of Twilio library within your CodeIgniter 3 project. Twilio is cloud communications platform for building SMS, Voice & Messaging applications.

Here are the steps to send SMS messages using Twilio APIs in the CodeIgniter 3 application:

Step 1: Download and install CodeIgniter.

Step 2: Run below composer command to download twilio/sdk library from your project folder. It will create a new folder called “vendor” and it will download twilio/sdk library into it.

$ composer require twilio/sdk

Here is my the directory structure after installing phpoffice/phpspreadsheet

Step 3: Open application/config/config.php file and set you vendor directory path

 $config['composer_autoload'] = 'vendor/autoload.php';

Step 4: Use twilio/sdk library inside in your controller, here is the sample:

defined('BASEPATH') OR exit('No direct script access allowed');
use Twilio\Rest\Client;

class Welcome extends CI_Controller {

	public function index()
		$data = ['phone' => '+919703132428', 'text' => 'Hello, CI'];

	protected function sendSMS($data) {
          // Your Account SID and Auth Token from
            $sid = 'your_sid';
            $token = 'your_token';
			$client = new Client($sid, $token);
            // Use the client to do fun stuff like send text messages!
             return $client->messages->create(
                // the number you'd like to send the message to
                    // A Twilio phone number you purchased at
                    "from" => "+your Twilio number",
                    // the body of the text message you'd like to send
                    'body' => $data['text']

Step 5: Let's run the application with PHP Built-in web server for testing, issue following command from the root of the project.

PS C:\Users\arjun\Desktop\ci-Twilio> php -S localhost:3030
PHP 7.1.6 Development Server started at Tue May 15 23:55:33 2018
Listening on http://localhost:3030

Step 6: Now if you access the http://localhost:3030 given number will receive the message/SMS.