callback - Twilio: Best method for retrieving media uri for sent sms -


when server sends sms mediaurl parameter specified, want utilize link location(s) twilio assigns image(s) construct img tags src properties based on media->uri.

up until today, following code working...

$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 $media) {     $attachment = $twilio_mediaurl . $media->uri;     $attachments .= "<br><br><a href='" . $attachment . "' target='_blank'><img src='" . $attachment . "' target='_blank'></a>"; } 

i'm not sure whether above shown technique no longer acceptable, or whether worked fluke. instance, perhaps status returned in timely fashion , doesn't wait delays in transferring images server twilio, , so, under heavy loads, no url's assigned until after status has been delivered.

  1. should above shown technique work? if not...
  2. are there parameters provided in callback status provide information? if not...
  3. what's best way retrieve information after callback?
  4. if url can reliably retrieved after callback, there way in original creation of message pass parameter returned server know mediaurl been specified message? (obviously, server use sid fetch message parameters local database, wouldn't efficient.)

a quick review , update of items in original question...

  • although method shown in question follows twilio documentation, results unrealiable. in testing, twilio server provided required links 60% of time. note of images tested 5kb, apparently not relate file size.
  • as shown in original question, wondered whether links accessible in status callback. don't believe are.
  • also shown in original question, wondered whether there in status callback indicate attachments sent. realized adjust own callback url accordingly, , review parameters when callback executed.

for completeness, here's entire process sending, receiving immediate update, receiving callback update, requesting more information twilio's server:

// append callback url if attachments. if (isset($_request["mediaurl"]) ) {     $callback .= "&attachments=true"; }  // prep info send. $params = array (     "to" => $to,     "from" => $from,     "body" => $body,     "statuscallback" => $callback );  // if upload has attachment array, include it. if (isset($_request["mediaurl"]) ) {     $params["mediaurl"] = $_request["mediaurl"]; }  // send twilio. $client = new services_twilio($twilio_sid, $twilio_token); $message = $client->account->messages->create($params);  // note initial/partial sid , status. $sid = $message->sid; $status = $message->status;  // if there attachment, fetch twilio links. // portion fails 40% of time in use. if (isset($_request["mediaurl"]) ) {     foreach ($message->media $media) {         $attachment = $twilio_mediaurl . $media->uri;         $body .= "<br><a href='" . $attachment . "' target='_blank'><img src='" . $attachment . "' target='_blank'></a>";     } } 

when message status changes, twilio's server contacts server based on callback url...

// respond twilio header("content-type: text/xml"); echo "<?xml version=\"1.0\" encoding=\"utf-8\"?><response></response>";  // read arguments. // cleanse data since we're not yet sure data coming 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 $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); } 

for trying this, i'm not saying it's best way; but, in absence of receiving response twilio, or finding information in documentation, best come with.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -