快速回顾和更新原始问题中的项目...
- 虽然我的问题中显示的方法遵循 Twilio 文档,但结果不可靠。在我的测试中,Twilio 服务器仅在 60% 的时间内提供了所需的链接。请注意,所有测试的图像都只有 5KB,因此它显然与文件大小无关。
- 如原始问题所示,我想知道在状态回调中是否可以访问链接。我不相信他们是。
- 也如原始问题所示,我想知道状态回调中是否有任何内容表明附件已发送。我意识到我可以相应地调整我自己的回调 url,然后在执行回调时查看这些参数。
为了完整起见,以下是从发送到接收即时更新,再到接收回调更新,再到从 Twilio 的服务器请求更多信息的整个过程:
// Append to the callback URL if attachments.
if (isset($_REQUEST["MediaUrl"]) ) {
$callback .= "&Attachments=true";
}
// Prep info to send.
$params = array (
"To" => $to,
"From" => $from,
"Body" => $body,
"StatusCallback" => $callback
);
// If the upload has an attachment array, include it.
if (isset($_REQUEST["MediaUrl"]) ) {
$params["MediaUrl"] = $_REQUEST["MediaUrl"];
}
// Send to Twilio.
$client = new Services_Twilio($twilio_sid, $twilio_token);
$message = $client->account->messages->create($params);
// Note the initial/partial SID and status.
$sid = $message->sid;
$status = $message->status;
// If there was an attachment, fetch the Twilio links.
// This is the portion that fails 40% of the time in my use.
if (isset($_REQUEST["MediaUrl"]) ) {
foreach ($message->media as $media) {
$attachment = $twilio_mediaURL . $media->uri;
$body .= "<br><a href='" . $attachment . "' target='_blank'><img src='" . $attachment . "' target='_blank'></a>";
}
}
当消息状态发生变化时,Twilio 的服务器会根据回调 URL 联系我的服务器...
// Immediately respond to Twilio
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response></Response>";
// Read the arguments.
// Cleanse the data since we're not yet sure the data really is coming from Twilio.
$sid = preg_replace("/\W|_/", "", $_REQUEST["MessageSid"]);
$to = preg_replace("/[^0-9+]/", "", $_REQUEST["To"]);
$status = preg_replace("/\W|_/", "", $_REQUEST["MessageStatus"]);
if (isset($_REQUEST["Attachments"]) ) {
require_once $twilio_source;
$client = new Services_Twilio($twilio_sid, $twilio_token);
$body = $client->account->messages->get($sid)->body;
foreach ($client->account->messages->get($sid)->media as $media) {
$attachment = $twilio_mediaURL . $media->uri;
$body .= "<br><a href='" . $attachment . "' target='_blank'><img src='" . $attachment . "' target='_blank'></a>";
}
updateMessage($sid, $to, $status, $body);
}
else {
updateMessage($sid, $to, $status, null);
}
对于任何尝试这样做的人,我并不是说这是最好的方法。但是,在没有收到 Twilio 的回复或在他们的文档中找到信息的情况下,这是我能想到的最好的方法。