Twilio - How to get media url from messages list?

1.7k views Asked by At

I am using this code to get the list of messages received. I like to get the media url of the image if the message is a MMS message. I can identify the MMS with num_media property.

I don't know how to the get media url. The document says about subresource_uris. I am not sure how to use this in this case.

  $client = new Services_Twilio($AccountSid, $AuthToken); 

  $messages = $client->account->messages->getIterator(0, 5, array()) ;
  $media = "";
  foreach($messages as $sms) {

      if ($sms->num_media > 0)
      {

      }


  } 
1

There are 1 answers

6
philnash On

Twilio developer evangelist here.

The media attached to a message can be found by querying the Media list resource for that message. That's nice and easy in the PHP library as you need to loop over the media resource for the $sms message. Like so:

  $client = new Services_Twilio($AccountSid, $AuthToken); 

  $messages = $client->account->messages->getIterator(0, 5, array()) ;
  $media = "";
  foreach($messages as $sms) {

      if ($sms->num_media > 0)
      {
          foreach($sms->media as $media) {
            echo $media->uri;
          }
      }


  } 

Let me know if that helps!