Reputation: 3515
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
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..
Upvotes: 6
Views: 8252
Reputation: 2522
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']."'
)";
Upvotes: 2
Reputation: 10366
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.
Upvotes: 3