Twilio:检索发送短信的媒体uri的最佳方法

时间:2015-06-13 23:57:44

标签: callback sms twilio

当我的服务器发送带有指定MediaUrl参数的SMS时,我想利用Twilio分配给图像的链接位置来构建具有基于media-> uri的src属性的img标签。

到目前为止,以下代码正在运行......

$client = new Services_Twilio($twilio_sid, $twilio_token);
$params = array (
    "To" => $to,
    "From" => $from,
    "Body" => $body,
    "MediaUrl" => $media,
    "StatusCallback" => $twilio_callbackURL
);
$message = $client->account->messages->create($params);

$sid = $message->sid;
$status = $message->status;
$attachments = "";
foreach ($message->media as $media) {
    $attachment = $twilio_mediaURL . $media->uri;
    $attachments .= "<br><br><a href='" . $attachment . "' target='_blank'><img src='" . $attachment . "' target='_blank'></a>";
}

我不确定上述技术是否不再可接受,或者它是否仅适用于侥幸。例如,也许状态总是及时返回,并且不会等待将图像从我的服务器传输到Twilio的任何延迟,因此,在重负载下,直到之后才分配URL。状态已经交付。

  1. 以上显示的技术是否有效?如果不是......
  2. 回调状态中是否提供了可提供此信息的参数?如果不是......
  3. 回调后检索此信息的最佳方法是什么?
  4. 如果只能在回调后可靠地检索URL,那么在原始创建消息时是否有一种方法可以传递将返回的参数,以便服务器知道是否为该消息指定了MediaUrl? (显然,服务器可以使用SID从其本地数据库中获取消息参数,但这样做效率不高。)

1 个答案:

答案 0 :(得分:0)

快速查看和更新​​原始问题中的项目......

  • 虽然我的问题中显示的方法遵循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的回复,或者在他们的文档中找到信息,这是我能想到的最好的。

相关问题