RSS 提要 https://moxie.foxnews.com/feedburner/world.xml 包含以下專案:
<link></link>
<title></title>
<media:group>
<media:content url="*.jpg" medium="image" isDefault="true"/>
<media:content url="*.jpg" medium="image" width="60" height="60"/>
</media:group>
<media:thumbnail url="*.jpg" width="60" height="60"/>
我知道如何顯示內容,<link>但<title>誰能幫我顯示里面的媒體 URL<media:group><media:content>和<media:thumbnail>?謝謝
uj5u.com熱心網友回復:
我已經清理了格式錯誤的 XML,因此它可以決議并提供一種查看錯誤的方法。
$xml = '<media:group>
<link></link>
<title></title>
<media:content url="*.jpg" medium="image" isDefault="true"></media:content>
<media:content url="*.jpg" medium="image" width="60" height="60"></media:content>
</media:group>';
libxml_use_internal_errors(true);
$doc = simplexml_load_string($xml);
if ($doc === false) {
$errors = libxml_get_errors();
print_r($errors);
} else {
print_r($doc);
}
結果
SimpleXMLElement Object
(
[link] => SimpleXMLElement Object
(
)
[title] => SimpleXMLElement Object
(
)
[media:content] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[url] => *.jpg
[medium] => image
[isDefault] => true
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[url] => *.jpg
[medium] => image
[width] => 60
[height] => 60
)
)
)
)
或直接從 URL
libxml_use_internal_errors(true);
$doc = simplexml_load_file('https://moxie.foxnews.com/feedburner/world.xml');
if ($doc === false) {
$errors = libxml_get_errors();
print_r($errors);
} else {
print_r($doc);
}
// 結果
SimpleXMLElement 物件([@attributes] => 陣列([version] => 2.0)
[channel] => SimpleXMLElement Object
(
[title] => FOX News : World
[description] => SimpleXMLElement Object
(
)
[image] => SimpleXMLElement Object
(
[url] => https://global.fncstatic.com/static/orion/styles/img/fox-news/logos/fox-news-desktop.png
[title] => FOX News : World
[link] => https://www.foxnews.com
)
[link] => https://www.foxnews.com
[item] => Array
(
[0] => SimpleXMLElement Object
(
[guid] => https://www.foxnews.com/world/us-taiwan-launch-trade-talks-biden-excludes-island-indo-pacific-group
[link] => https://www.foxnews.com/world/us-taiwan-launch-trade-talks-biden-excludes-island-indo-pacific-group
[category] => Array
(
[0] => c8e27233-08e3-5b74-9eff-da5e9fe271ff
[1] => fox-news/world/world-regions/asia
[2] => fox-news/world/world-regions/china
[3] => fox-news/person/joe-biden
[4] => fox-news/politics/foreign-policy
[5] => fnc
[6] => fox-news/world
[7] => article
[8] => Reuters
)
[title] => US, Taiwan to launch trade talks after Biden excludes island from Indo-Pacific group
[description] => SimpleXMLElement Object
(
)
[pubDate] => Wed, 01 Jun 2022 15:25:51 GMT
感謝您的澄清。
$xml = simplexml_load_file('https://moxie.foxnews.com/feedburner/world.xml');
foreach ($xml->channel->item as $item) {
foreach ($item->children('media', true) as $mg) {
$media_group = $mg->content->attributes()['url'];
print_r($media_group);
}
}
// 結果
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/06/lech-walesa.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/06/taiwan-us-flag.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/05/Putin.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/05/GettyImages-1395351622-1-e1651678769232.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/04/Dmitry-Peskov.png
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/06/Ukraine-President-Volodymyr-Zelenskyy-Russia-War.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/04/Israeli-Soldier.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2021/11/chinaforeignministryspokesman.jpg
)
SimpleXMLElement Object
(
[0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/02/zelenskyy-biden-split-2-25-22.jpg
)
更新
$rss = simplexml_load_file('https://moxie.foxnews.com/feedburner/world.xml');
foreach ($rss->channel->item as $item) {
echo '<p><a href="' . $item->link . '">' . $item->title . "</a></p>";
echo "<p>" . $item->description . "</p>";
foreach ($item->children('media', true) as $mg) {
foreach ($mg->children('media', true) as $c) {
echo '<img src="' . $c->attributes()['url'] . '"><br>';
}
}
}
// 輸出(注意第二張圖片是縮略圖)
<p>
<a href="https://www.foxnews.com/world/russia-global-food-crisis-49-million-famine-starvation-expert">Russia-induced global food crisis pushes 49M to 'brink' of famine, starvation, expert warns</a>
</p>
<p>Russia's war in Ukraine has sparked a global food crisis with 49 million facing famine. Now, experts are worried the world faces an increased security threat.</p>
<img src="https://static.foxnews.com/foxnews.com/content/uploads/2021/09/World-Food-Program-Kabul-Afghanistan..jpg"><br>
<img src="https://a57.foxnews.com/static.foxnews.com/foxnews.com/content/uploads/2021/09/60/60/World-Food-Program-Kabul-Afghanistan..jpg?ve=1&tl=1">
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/491870.html
