简体   繁体   English

接收SMS并使用Twilio将其存储在数据库中

[英]Receiving SMS and storing it in database using Twilio

i am using Twilio API to send and receive sms from the customers. 我正在使用Twilio API向客户发送和接收短信。

Every time i send the sms to my customers, i store the feilds like to , body in to my database. 每次我的手机短信发送到我的客户,我喜欢的精密组件存储tobody在我的数据库。

I have implemented the API for send message that works fine and am simply saving the fields in to my database. 我已经实现了发送消息的API,工作正常,只是将字段保存到我的数据库中。

My Problem 我的问题

When i receive SMS from my customers to my twilio number. 当我从我的客户收到我的twilio号码的短信。 i want to get the fields like from number and the body and save to my databse. 我想得到from numberbody的字段from number并保存到我的数据库。

i looked that the documentation here 我看了这里的文档

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

https://www.twilio.com/blog/2012/04/get-started-with-twilio-sms-receiving-incoming-sms-quickstart.html 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. 收到短信时,有人可以帮助我from numbermessage body获取。 tnx.. TNX ..

Twilio evangelist here. Twilio福音传教士在这里。

Twilio passes parameters in its HTTP request as form-encoded values, so you just need to use the REQUEST object to grab them: Twilio将HTTP请求中的参数作为表单编码值传递,因此您只需使用REQUEST对象来获取它们:

$from = $_REQUEST['From']

The SMS Quickstart for PHP has a more detailed example. SMS Quickstart for PHP有一个更详细的例子。

Hope hat helps. 希望帽子有帮助。

The documentation for Twilio SMS responses is here: https://www.twilio.com/docs/api/twiml/sms/twilio_request Twilio SMS响应的文档在这里: 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收到一个Twilio号码的消息时,它会对为该号码配置的消息URL发出同步HTTP请求,并期望接收响应的TwiML。 Twilio sends the following parameters with its request as POST parameters or URL query parameters, depending on which HTTP method you've configured. Twilio将其请求作为POST参数或URL查询参数发送以下参数,具体取决于您配置的HTTP方法。

You should simply have the data fields inside of PHP's $_REQUEST[] variable. 您应该只在PHP的$_REQUEST[]变量中包含数据字段。

$_REQUEST['MessageSid'] - A 34 character unique identifier for the message. $_REQUEST['MessageSid'] - 消息的34个字符唯一标识符。 May be used to later retrieve this message from the REST API. 可用于稍后从REST API检索此消息。

$_REQUEST['SmsSid'] - Same value as MessageSid. $_REQUEST['SmsSid'] - 与MessageSid值相同。 Deprecated and included for backward compatibility. 为了向后兼容而弃用并包含在内。

$_REQUEST['AccountSid'] - The 34 character id of the Account this message is associated with. $_REQUEST['AccountSid'] - 与此消息关联的帐户的34个字符ID。

$_REQUEST['From'] - The phone number that sent this message. $_REQUEST['From'] - 发送此消息的电话号码。

$_REQUEST['To'] - The phone number of the recipient. $_REQUEST['To'] - 收件人的电话号码。

$_REQUEST['Body'] - The text body of the message. $_REQUEST['Body'] - 消息的文本正文。 Up to 1600 characters long. 最多1600个字符。

$_REQUEST['NumMedia'] - The number of media items associated with your message $_REQUEST['NumMedia'] - 与您的消息关联的媒体项目数

Here is an example query you might use with a MySQL database. 以下是您可能与MySQL数据库一起使用的示例查询。 You should also be sending back a proper TWIXML response to Twilio and scrub the received data before running a query like this. 您还应该向Twilio发回适当的TWIXML响应,并在运行此类查询之前清理接收到的数据。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM