Perl Requires Chilkat v11.0.0+
Perl
Download the media files from a Twilio MMS
See more Twilio Examples
Supply the Message SID of a received or sent MMS. List its media records, then request each media resource without the .json extension to download the file bytes. Twilio may return a temporary HTTPS download URL. The example uses a separate HTTP client for that URL so the Twilio API key stays with the API host.
Run in an empty output directory. Files are written as mms-media-1.bin, mms-media-2.bin, and so on. The code prints the media SID and MIME type alongside each filename. An empty media list produces a count of zero.
Chilkat Perl Downloads
use strict;
use warnings;
use Chilkat2;
my $success = 0;
# Assumes Chilkat is unlocked. See Global Unlock Sample.
# Load an API key for the Twilio account from private application configuration.
my $http = Chilkat2::Http->new;
$http->RequireSslCertVerify(1);
$http->BasicAuth(1);
$http->Login('YOUR_API_KEY_SID');
$http->Password('YOUR_API_KEY_SECRET');
my $url = Chilkat2::StringBuilder->new;
$url->Append('https://api.twilio.com/2010-04-01/Accounts/');
$url->Append('YOUR_ACCOUNT_SID');
$url->Append('/Messages');
$url->Append('/');
$url->Append('YOUR_MESSAGE_SID');
$url->Append('/Media');
my $mediaBase = Chilkat2::StringBuilder->new;
$mediaBase->AppendSb($url);
$url->Append('.json');
# Handle the API redirect explicitly so a signed download uses no Twilio credentials.
$http->FollowRedirects(0);
my $download = Chilkat2::Http->new;
$download->RequireSslCertVerify(1);
my $fileResp = Chilkat2::HttpResponse->new;
my $fileUrl = Chilkat2::StringBuilder->new;
my $filename = Chilkat2::StringBuilder->new;
my $mediaSid = Chilkat2::StringBuilder->new;
my $saved = 0;
my $resp = Chilkat2::HttpResponse->new;
my $reply = Chilkat2::JsonObject->new;
my $more = 1;
while ($more) {
$success = $http->HttpNoBody('GET', $url->GetAsString(), $resp);
if ($resp->StatusCode == 0) {
print "No HTTP response from Twilio.\n";
exit;
}
my $parsed = $reply->Load($resp->BodyStr);
if ((!$success) || ($resp->StatusCode != 200)) {
print 'Twilio returned HTTP ', $resp->StatusCode, "\n";
if (($parsed) && ($reply->JsonTypeOf('code') == 2)) {
print 'Twilio error: ', $reply->IntOf('code'), ' ', $reply->StringOf('message'), "\n";
}
exit;
}
if (!$parsed) {
print "Expected a JSON response from Twilio.\n";
exit;
}
if ($reply->JsonTypeOf('media_list') != 4) {
print "Expected an array of media items.\n";
exit;
}
my $count = $reply->SizeOfArray('media_list');
my $i = 0;
while ($i < $count) {
$reply->I($i);
$mediaSid->SetString($reply->StringOf('media_list[i].sid'));
if ($mediaSid->Length == 0) {
print "Media item is missing its SID.\n";
exit;
}
$fileUrl->SetString($mediaBase->GetAsString());
$fileUrl->Append('/');
$fileUrl->AppendSb($mediaSid);
$success = $http->HttpNoBody('GET', $fileUrl->GetAsString(), $fileResp);
if (($fileResp->StatusCode >= 300) && ($fileResp->StatusCode < 400)) {
$fileUrl->SetString($fileResp->GetHeaderField('Location'));
if (!$fileUrl->StartsWith('https://', 0)) {
print "Expected an HTTPS media download URL.\n";
exit;
}
$success = $download->HttpNoBody('GET', $fileUrl->GetAsString(), $fileResp);
}
if ((!$success) || ($fileResp->StatusCode != 200)) {
print 'Media download failed for ', $mediaSid->GetAsString(), '. HTTP ', $fileResp->StatusCode, "\n";
exit;
}
$filename->SetString('mms-media-');
$filename->AppendInt($saved + 1);
$filename->Append('.bin');
$success = $fileResp->SaveBodyBinary($filename->GetAsString());
if (!$success) {
print $fileResp->LastErrorText, "\n";
exit;
}
print $filename->GetAsString(), ' ', $mediaSid->GetAsString(), ' ', $reply->StringOf('media_list[i].content_type'), "\n";
$saved = $saved + 1;
$i = $i + 1;
}
if ($reply->IsNullOf('next_page_uri')) {
$more = 0;
} else {
$url->SetString($reply->StringOf('next_page_uri'));
if (!$url->StartsWith('/2010-04-01/Accounts/', 1)) {
print "Expected a Twilio relative next-page URI.\n";
exit;
}
$url->Prepend('https://api.twilio.com');
}
}
print 'Media files saved: ', $saved, "\n";