当我的服务器发送带有指定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。状态已经交付。
答案 0 :(得分:0)
快速查看和更新原始问题中的项目......
为了完整起见,这是从发送,接收即时更新,接收回叫更新到从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的回复,或者在他们的文档中找到信息,这是我能想到的最好的。