PHP/XML:如果服务器上没有映像,则用另一个url替换映像src
我的代码:PHP/XML:如果服务器上没有映像,则用另一个url替换映像src,php,xml,Php,Xml,我的代码: <?php $Length = 25; $rnd = substr(str_shuffle(md5(time())), 0, $Length); $songs = []; $channels = range(1,6); // Download current songs (from 1 to 6) foreach ($channels as $sid) { $song = file_get_contents(sprintf('http://95.154.254.
<?php
$Length = 25;
$rnd = substr(str_shuffle(md5(time())), 0, $Length);
$songs = [];
$channels = range(1,6);
// Download current songs (from 1 to 6)
foreach ($channels as $sid) {
$song = file_get_contents(sprintf('http://95.154.254.129:17618/currentsong?sid=%s', $sid));
list($artist, $title) = explode('-', $song, 2);
$songs[] = [
'rnd' => $rnd,
'sid' => $sid,
'image' => sprintf('http://95.154.254.129:17618/playingart?sid=%s&rnd=%s', $sid, $rnd),
'song' => $song,
'artist' => trim($artist),
'title' => trim($title)
];
}
unset($song);
//print_r($songs);
// Write new xml file
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$allChannels = $doc->createElement("allchannels");
$allChannels->setAttribute('count' , count($channels));
$doc->appendChild($allChannels);
$trackInfo = $doc->createElement("ilr_trackinfo");
$trackInfo->setAttribute('channel' , $song['sid']);
foreach ($songs as $song) {
$trackInfo = $doc->createElement("ilr_trackinfo");
$trackInfo->setAttribute('channel' , $song['sid']);
$allChannels->appendChild($trackInfo);
$artist = $doc->createElement("artist");
$trackInfo->appendChild($artist);
$artistCdata = $doc->createCDATASection($song['artist']);
$artist->appendChild($artistCdata);
$title = $doc->createElement("title");
$trackInfo->appendChild($title);
$titleCdata = $doc->createCDATASection($song['title']);
$title->appendChild($titleCdata);
$image = $doc->createElement("image");
$image->setAttribute('src', $song['image']);
$trackInfo->appendChild($image);
}
header("Content-type: text/xml");
echo $doc->saveXML();
?>
如果图像不可用,我需要将图像替换为:“”
非常感谢您的帮助!:)强>
我需要添加文本。。。Lorem ipsum dolor sit amet,Conseteur sadipscing Eliter,sed diam Nonum eirmod Temporal invidunt ut Labour et dolore magna aliquyam.这里是一种方法
foreach ($channels as $sid) {
$song = file_get_contents(sprintf('http://95.154.254.129:17618/currentsong?sid=%s', $sid));
list($artist, $title) = explode('-', $song, 2);
$actualImageUrl = sprintf('http://95.154.254.129:17618/playingart?sid=%s&rnd=%s', $sid, $rnd);
$placeholderImageUrl = "http://www.reyfm.de/img/nocover.png";
$imageUrl = @getimagesize($actualImageUrl) ? $actualImageUrl : $placeholderImageUrl;
$songs[] = [
'rnd' => $rnd,
'sid' => $sid,
'image' => $imageUrl,
'song' => $song,
'artist' => trim($artist),
'title' => trim($title)
];
}
以下是一种方法您可以如何做到这一点
foreach ($channels as $sid) {
$song = file_get_contents(sprintf('http://95.154.254.129:17618/currentsong?sid=%s', $sid));
list($artist, $title) = explode('-', $song, 2);
$actualImageUrl = sprintf('http://95.154.254.129:17618/playingart?sid=%s&rnd=%s', $sid, $rnd);
$placeholderImageUrl = "http://www.reyfm.de/img/nocover.png";
$imageUrl = @getimagesize($actualImageUrl) ? $actualImageUrl : $placeholderImageUrl;
$songs[] = [
'rnd' => $rnd,
'sid' => $sid,
'image' => $imageUrl,
'song' => $song,
'artist' => trim($artist),
'title' => trim($title)
];
}