使用 Twilio 接收短信并将其存储在数据库中 [英] Receiving SMS and storing it in database using Twilio

查看:39
本文介绍了使用 Twilio 接收短信并将其存储在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Twilio API 发送和接收来自客户的短信.

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

每次我向客户发送短信时,我都会将诸如 tobody 之类的字段存储到我的数据库中.

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

我已经实现了用于发送消息的 API,该 API 工作正常,并且只是将字段保存到我的数据库中.

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

我的问题

当我收到客户发送到我的 twilio 号码的短信时.我想获取诸如 from numberbody 之类的字段并保存到我的数据库中.

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.

我看了这里的文档

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.

有人可以帮我在收到短信时获取from number消息正文.tnx..

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

推荐答案

Twilio SMS 响应的文档在这里:https://www.twilio.com/docs/api/twiml/sms/twilio_request

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

这是一个相关的引用:

当 Twilio 收到您的一个 Twilio 号码的消息时对为此配置的消息 URL 的同步 HTTP 请求编号,并期望收到 TwiML 作为响应.Twilio 发送以下参数及其请求作为 POST 参数或 URL 查询参数,具体取决于您配置的 HTTP 方法.

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.

您应该只在 PHP 的 $_REQUEST[] 变量中包含数据字段.

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

$_REQUEST['MessageSid'] - 消息的 34 个字符的唯一标识符.可用于稍后从 REST API 检索此消息.

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

$_REQUEST['SmsSid'] - 与 MessageSid 相同的值.已弃用并包含在内以实现向后兼容性.

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

$_REQUEST['AccountSid'] - 与此消息关联的帐户的 34 个字符 ID.

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

$_REQUEST['From'] - 发送此消息的电话号码.

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

$_REQUEST['To'] - 收件人的电话号码.

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

$_REQUEST['Body'] - 消息的正文.最多 1600 个字符.

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

$_REQUEST['NumMedia'] - 与您的消息相关联的媒体项目数量

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

这是一个您可能会在 MySQL 数据库中使用的示例查询.您还应该将正确的 TWIXML 响应发送回 Twilio 并在运行这样的查询之前清理接收到的数据.

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 接收短信并将其存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆