5
votes

i am using Twilio API to send and receive sms from the customers.

Every time i send the sms to my customers, i store the feilds like to, body in to my database.

I have implemented the API for send message that works fine and am simply saving the fields in to my database.

My Problem

When i receive SMS from my customers to my twilio number. i want to get the fields like from number and the body and save to my databse.

i looked that the documentation here

https://www.twilio.com/docs/api/twiml

https://www.twilio.com/blog/2012/04/get-started-with-twilio-sms-receiving-incoming-sms-quickstart.html

But this only shows how to send a response to customer when a message is received.

I want to save the received sms in to my database.

Can someone help me top get the from number and the message body when a SMS is received. tnx..

2
Can you add a code snippet of what you have written that isn't working so we can help you debug it?Kmeixner
If their docs don't help - try checking the value of incoming parameters by getting their test going but logging the value of $_REQUEST. This will show you what fields they may or may not be sending for you to log.ahoffner

2 Answers

1
votes

The documentation for Twilio SMS responses is here: https://www.twilio.com/docs/api/twiml/sms/twilio_request

Here is a relevant quote:

When Twilio receives a message for one of your Twilio numbers it makes a synchronous HTTP request to the message URL configured for that number, and expects to receive TwiML in response. Twilio sends the following parameters with its request as POST parameters or URL query parameters, depending on which HTTP method you've configured.

You should simply have the data fields inside of PHP's $_REQUEST[] variable.

$_REQUEST['MessageSid'] - A 34 character unique identifier for the message. May be used to later retrieve this message from the REST API.

$_REQUEST['SmsSid'] - Same value as MessageSid. Deprecated and included for backward compatibility.

$_REQUEST['AccountSid'] - The 34 character id of the Account this message is associated with.

$_REQUEST['From'] - The phone number that sent this message.

$_REQUEST['To'] - The phone number of the recipient.

$_REQUEST['Body'] - The text body of the message. Up to 1600 characters long.

$_REQUEST['NumMedia'] - The number of media items associated with your message

Here is an example query you might use with a MySQL database. You should also be sending back a proper TWIXML response to Twilio and scrub the received data before running a query like this.

$sql = "INSERT INTO messages (sid, from, body) 
        VALUES (
                 '".$_REQUEST['MessageSid']."',
                 '".$_REQUEST['From']."',
                 '".$_REQUEST['Body']."'
               )";
2
votes

Twilio evangelist here.

Twilio passes parameters in its HTTP request as form-encoded values, so you just need to use the REQUEST object to grab them:

$from = $_REQUEST['From']

The SMS Quickstart for PHP has a more detailed example.

Hope hat helps.