【问题标题】:Twilio: Best method for retrieving media uri for sent smsTwilio:检索已发送短信的媒体 uri 的最佳方法
【发布时间】:2015-06-13 23:57:44
【问题描述】:

当我的服务器发送带有指定 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 从其本地数据库中获取消息参数,但这不会那么有效。)

【问题讨论】:

    标签: callback sms twilio


    【解决方案1】:

    快速回顾和更新原始问题中的项目...

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多