Category Covered

Integrating CakePHP with Twillio

How you will integrate twillio on cakephp? Here i am going to write step by step.

Step1: Download the twillio from the below link and extract it to projectName/app/vendor

Directly you may download from the given links https://github.com/twilio/twilio-php/archive/master.zip or

https://github.com/twilio/twilio-php

Step2: create a controller twillio_controller.php with the following content

App::import('Vendor', 'Twilio', array('file' => 'twilio-php-master'.DS.'Services'.DS.'Twilio.php'));
//- See more at: http://blog.jambura.com/2012/06/20/managing-conference-calls-with-twilio-api-in-cakephp/#sthash.Rmv9eGj2.dpuf

class TwiliosController extends AppController {

    function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->authorize = 'controller';
        $this->Auth->allow('attempt_call','call_tracking', 'send_text', 'get_account_info', 'get_list_of_accounts', 'call', 'screen_for_machine', 'complete_call');
        $this->layout="default";
        $this->Auth->fields = array('username' => 'username', 'password' => 'password');
        $this->Auth->autoRedirect = false;
    }
    
        var $uses = array("Call");
    
        //Trial account arafatix@yahoo.com
        var $sid = 'AC7bf1b33ad470159107c01b8ef5704221';
        var $token = '8d0cd85dda9c5c5ca85f9d73c1a2b2e3';
        
        
    
    //initialize twilio services
    public function send_text() {
        $client = new Services_Twilio($this->sid, $this->token);
        $send_to = '+880 1922291533';
        $message_text = 'This is test sms using test cred';
        $message = $client->account->sms_messages->create($this->twilio_number, $send_to, $message_text);
        echo $message->date_created."<br/>";
        echo $message->to."<br/>";
        echo $message->from."<br/>";
        echo $message->body."<br/>";
    }
    
    function get_account_info(){
        $client = new Services_Twilio($this->sid, $this->token);
        //debug($client->account);
        $account = $client->accounts->get($this->sid);
        echo $account->date_created;
        
        
        
        
        exit;
    }
    
    function get_list_of_accounts(){
        $client = new Services_Twilio($this->sid, $this->token);
        foreach ($client->accounts as $account) {
            echo $account->sid;
        }
        exit;
    }
    
    // initialize twilio services
    public function call() {
        /*$client = new Services_Twilio($this->sid, $this->token);
        $send_to = '+880 1922291533';
        
        $call = $client->account->calls->create($this->twilio_number, $send_to, "https://demo.twilio.com/welcome/voice/", array());

        echo $call->sid;
        echo $call->DialCallDuration;
        echo $call->RecordingUrl;
        exit;*/
        //echo $call->date_created."<br/>";
        //echo $call->to."<br/>";
        //echo $call->from."<br/>";
        //echo $call->body."<br/>";
    }
    
    function attempt_call(){
        $this->layout = "blank";
        $this->loadModel("Call");
    
        if( !empty($_REQUEST) ){
            $this->Call->call_save_update($_REQUEST);
        }
        //debug($this->params["url"]["DialCallStatus"]);exit;
        //Set multiple real nubmers to call
        //$numbers = array("<number to call 1>", "<number to call 2>", "<number to call n>");
        $numbers = array("813-325-4340"); //Real number of Brent
        $number_index = isset($this->params["url"]["number_index"]) ? $this->params["url"]["number_index"] : "0";
        $DialCallStatus = isset($this->params["url"]["DialCallStatus"]) ? $this->params["url"]["DialCallStatus"] : "";
        //$this->RequestHandler->respondAs('xml');
        $this->set("DialCallStatus",$DialCallStatus);
        $this->set("number_index",$number_index);
        $this->set("numbers", $numbers);
    }
    
    function screen_for_machine(){
        $this->layout = "blank";
    }
    
    function complete_call(){
        $this->layout = "blank";
    }
    
    function call_tracking(){
        $this->loadModel("Call");
        $this->paginate = array('limit'=>20,'order'=>'Call.created DESC');
        $this->set('calls', $this->paginate( $this->Call ));
    }
}

Step3: Create a view folder named "twillios"