Twilio Receiving SMS codeigniter

1.6k views Asked by At

Hi iam using twilio to send and receive SMS from my customers. The sending SMS works fine.

When a SMS is received i want to save from, Body to my database.its not working. Here is my code.

Controller

function  receive(){
    $post_data['from']       =  $this->input->post('From');
    $post_data['body']       =  $this->input->post('Body');

    if($post_data['from'] && $post_data['body']){
        $this->receive->insert_received_message($post_data);
    }

}

Model

function insert_received_message($data){

     $sms['pone_number']    = $data['from'];
     $sms['message_type']   = 2;
     $sms['body']           = $data['body'];

     $results = $this->db->insert('Sms_message', $sms); 
     return $results;
}

I added the url to my number like this

enter image description here

Error message in Received log

enter image description here

Can someone help me to fix this. TNX.

1

There are 1 answers

1
Saty On

Your data is not save into database make sure your filed name is correct in your array

Controller

function receive() {
    $from = $this->input->post('From');
    $body = $this->input->post('Body');

    if (isset($from) && isset($body)) { //create your insert array like that
        $sms = array(
            'pone_number' => $from,
            'message_type' => 2,
            'body' => $body
        );

        $this->receive->insert_received_message($sms);
    }
}

Model

function insert_received_message($data){

     $this->db->insert('Sms_message', $data); 
     if($this->db->insert_id()>0)// check last insert id
     {
         return $this->db->insert_id();
     }
     else{
         return FALSE;
     }

}

The following link also demonstrates how to interact with messages received.

https://www.twilio.com/docs/quickstart/php/sms/replying-to-sms-messages