PHP ActiveX Requires Chilkat v11.0.0+
PHP ActiveX
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 ActiveX Downloads
<?php
$success = 0;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Load an API key for the Twilio account from private application configuration.
$http = new COM("Chilkat.Http");
$http->RequireSslCertVerify = 1;
$http->BasicAuth = 1;
$http->Login = 'YOUR_API_KEY_SID';
$http->Password = 'YOUR_API_KEY_SECRET';
$url = new COM("Chilkat.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 COM("Chilkat.StringBuilder");
$mediaBase->AppendSb($url);
$url->Append('.json');
// Handle the API redirect explicitly so a signed download uses no Twilio credentials.
$http->FollowRedirects = 0;
$download = new COM("Chilkat.Http");
$download->RequireSslCertVerify = 1;
$fileResp = new COM("Chilkat.HttpResponse");
$fileUrl = new COM("Chilkat.StringBuilder");
$filename = new COM("Chilkat.StringBuilder");
$mediaSid = new COM("Chilkat.StringBuilder");
$saved = 0;
$resp = new COM("Chilkat.HttpResponse");
$reply = new COM("Chilkat.JsonObject");
$more = 1;
while (($more == 1)) {
$success = $http->HttpNoBody('GET',$url->getAsString(),$resp);
if ($resp->StatusCode == 0) {
print 'No HTTP response from Twilio.' . "\n";
exit;
}
$parsed = $reply->Load($resp->BodyStr);
if (($success != 1) or ($resp->StatusCode != 200)) {
print 'Twilio returned HTTP ' . $resp->StatusCode . "\n";
if (($parsed == 1) and ($reply->JsonTypeOf('code') == 2)) {
print 'Twilio error: ' . $reply->IntOf('code') . ' ' . $reply->stringOf('message') . "\n";
}
exit;
}
if ($parsed != 1) {
print 'Expected a JSON response from Twilio.' . "\n";
exit;
}
if ($reply->JsonTypeOf('media_list') != 4) {
print '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) {
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) and ($fileResp->StatusCode < 400)) {
$fileUrl->SetString($fileResp->getHeaderField('Location'));
if ($fileUrl->StartsWith('https://',0) != 1) {
print 'Expected an HTTPS media download URL.' . "\n";
exit;
}
$success = $download->HttpNoBody('GET',$fileUrl->getAsString(),$fileResp);
}
if (($success != 1) or ($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 == 0) {
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') == 1) {
$more = 0;
}
else {
$url->SetString($reply->stringOf('next_page_uri'));
if ($url->StartsWith('/2010-04-01/Accounts/',1) != 1) {
print 'Expected a Twilio relative next-page URI.' . "\n";
exit;
}
$url->Prepend('https://api.twilio.com');
}
}
print 'Media files saved: ' . $saved . "\n";
?>