PHP Requires Chilkat v11.0.0+
PHP
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 PHP Downloads
<?php
use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;
use Chilkat2\StringBuilder;
$success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Load an API key for the Twilio account from private application configuration.
$http = new Http();
$http->RequireSslCertVerify = true;
$http->BasicAuth = true;
$http->Login = 'YOUR_API_KEY_SID';
$http->Password = 'YOUR_API_KEY_SECRET';
$url = new StringBuilder();
$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');
$mediaBase = new StringBuilder();
$mediaBase->AppendSb($url);
$url->Append('.json');
// Handle the API redirect explicitly so a signed download uses no Twilio credentials.
$http->FollowRedirects = false;
$download = new Http();
$download->RequireSslCertVerify = true;
$fileResp = new HttpResponse();
$fileUrl = new StringBuilder();
$filename = new StringBuilder();
$mediaSid = new StringBuilder();
$saved = 0;
$resp = new HttpResponse();
$reply = new JsonObject();
$more = true;
while ($more) {
try {
$http->HttpNoBody('GET', $url->GetAsString(), $resp);
$success = true;
} catch (\Chilkat2\Exception $e) {
$success = false;
}
if ($resp->StatusCode == 0) {
echo "No HTTP response from Twilio.\n";
exit;
}
try {
$reply->Load($resp->BodyStr);
$parsed = true;
} catch (\Chilkat2\Exception $e) {
$parsed = false;
}
if ((!$success) || ($resp->StatusCode != 200)) {
echo 'Twilio returned HTTP ', $resp->StatusCode, "\n";
if (($parsed) && ($reply->JsonTypeOf('code') == 2)) {
echo 'Twilio error: ', $reply->IntOf('code'), ' ', $reply->StringOf('message'), "\n";
}
exit;
}
if (!$parsed) {
echo "Expected a JSON response from Twilio.\n";
exit;
}
if ($reply->JsonTypeOf('media_list') != 4) {
echo "Expected an array of media items.\n";
exit;
}
$count = $reply->SizeOfArray('media_list');
$i = 0;
while ($i < $count) {
$reply->I = $i;
$mediaSid->SetString($reply->StringOf('media_list[i].sid'));
if ($mediaSid->Length == 0) {
echo "Media item is missing its SID.\n";
exit;
}
$fileUrl->SetString($mediaBase->GetAsString());
$fileUrl->Append('/');
$fileUrl->AppendSb($mediaSid);
$http->HttpNoBody('GET', $fileUrl->GetAsString(), $fileResp);
if (($fileResp->StatusCode >= 300) && ($fileResp->StatusCode < 400)) {
$fileUrl->SetString($fileResp->GetHeaderField('Location'));
if (!$fileUrl->StartsWith('https://', false)) {
echo "Expected an HTTPS media download URL.\n";
exit;
}
try {
$download->HttpNoBody('GET', $fileUrl->GetAsString(), $fileResp);
$success = true;
} catch (\Chilkat2\Exception $e) {
$success = false;
}
}
if ((!$success) || ($fileResp->StatusCode != 200)) {
echo 'Media download failed for ', $mediaSid->GetAsString(), '. HTTP ', $fileResp->StatusCode, "\n";
exit;
}
$filename->SetString('mms-media-');
$filename->AppendInt($saved + 1);
$filename->Append('.bin');
try {
$fileResp->SaveBodyBinary($filename->GetAsString());
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo $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 = false;
} else {
$url->SetString($reply->StringOf('next_page_uri'));
if (!$url->StartsWith('/2010-04-01/Accounts/', true)) {
echo "Expected a Twilio relative next-page URI.\n";
exit;
}
$url->Prepend('https://api.twilio.com');
}
}
echo 'Media files saved: ', $saved, "\n";