Receiving SMS and storing it in database using Twilio

北城以北 提交于 2019-12-05 13:09:52

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']."'
               )";

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!