以下 PHP 示例列出并可选择删除 SMS 和 MMS 消息以及关联的媒体附件。
在下面的示例中,指定了“from”和“to”数字,并且
$delete
设置为 false
。
$client = new Client($twilio_sid, $twilio_token);
$params = array(
'from' => '+15555555555',
'to' => '+16666666666'
);
$delete = false;
$smsCounter = 0;
$mmsCounter = 0;
$mediaCounter = 0;
echo "<div style='font-family:monospace;'>";
foreach ($client->messages->read($params) as $message) {
if (substr($message->sid, 0, 1) == "S") {
$smsCounter++;
echo "SMS #{$smsCounter} SID: {$message->sid} from {$message->from} to {$message->to}.<br>";
}
else {
$mmsCounter++;
echo "MMS #{$mmsCounter} SID: {$message->sid} from {$message->from} to {$message->to}.<br>";
$allMedia = $client->messages($message->sid)->media->read();
if (count($allMedia) > 0) {
foreach ($allMedia as $media) {
$mediaCounter++;
echo " Attachment SID: {$media->sid}.<br>";
if ($delete) {
$media->delete($media->sid);
}
}
}
else {
echo " No attachments found.<br>";
}
}
if ($delete) {
$message->delete($message->sid);
}
}
echo "<br>{$smsCounter} SMS message" . ($smsCounter === 1 ? "" : "s") . " found" . ($delete ? "and deleted." : ".");
echo "<br>{$mmsCounter} multimedia message" . ($mmsCounter === 1 ? "" : "s") ." found" . ($delete ? "and deleted." : ".");
echo "<br>{$mediaCounter} media attachment" . ($mediaCounter === 1 ? "" : "s") ." found" . ($delete ? "and deleted." : ".");
要同时删除消息和关联的媒体附件,可以将
$delete
设置为 true
。
对于与帐户关联的所有消息和媒体的列表,可以从第一个
$params
中省略 foreach
,以便该行显示如下:
foreach ($client->messages->read() as $message) {