简体   繁体   English

解析媒体:来自RSS feed的内容与PHP

[英]Parse media:content from RSS feed with PHP

I am trying to parse media:content from RSS with PHP and then show it using HTML. 我正在尝试解析媒体:使用PHP从RSS中获取内容,然后使用HTML显示它。

I went through numerous posts on the same topic but since i'm a beginner I couldn't figure it out because the codes were different from mine. 我在同一个主题上经历了很多帖子,但由于我是初学者,我无法弄明白,因为代码与我的不同。

Currently I dont' have any line that is trying to get the image from xml. 目前我没有任何试图从xml获取图像的行。

<?php
$html = "";
$url = "url.rss";

$xml = simplexml_load_file($url);
$namespaces = $xml->getNamespaces(true);

for($i = 0; $i < 50; $i++){
    $title = $xml->channel->item[$i]->title;
    $link = $xml->channel->item[$i]->link;
    $description = $xml->channel->item[$i]->description;
    $pubDate = $xml->channel->item[$i]->pubDate;
    $author = $xml->channel->item[$i]->author;


    $html .= "<a href='$link'><h3>$title</h3></a>";
    $html .= "$description";
    $html .= "<p>$pubDate</p>";
    $html .= "<p>$author</p><hr>";

}
echo $html;
?>

This is the info I need from the XML file: 这是我需要的XML文件信息:

 <media:content url="www.image.jpg" medium="image" type="image/jpeg"     width="850" height="425" />

Thanks! 谢谢!

I expect the PHP file to show the media file. 我希望PHP文件显示媒体文件。

Can you please give us more info? 你能告诉我们更多信息吗? What is the value of '$xml' after you run simplexml_load_file? 运行simplexml_load_file后'$ xml'的值是多少? (did you get the correct data?) (你得到了正确的数据吗?)

What error message did you get? 你得到了什么错误信息?

EDIT - according to your comment 编辑 - 根据您的评论

try using 尝试使用

$xml->channel->item[$i]->children('media', true)->content->attributes();

The 'media' inside children is the namespace for the 'content' element. 子内的'media'是'content'元素的命名空间。 While the boolean 'true' variable tells the parser to refer the 'media' as a namespace 而boolean'true'变量告诉解析器将'media'称为命名空间

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM