【问题标题】:Parse media:content from RSS feed with PHP使用 PHP 解析媒体:来自 RSS 提要的内容
【发布时间】:2019-06-11 06:18:02
【问题描述】:

我正在尝试使用 PHP 解析来自 RSS 的媒体:内容,然后使用 HTML 显示它。

我浏览过很多关于同一主题的帖子,但由于我是初学者,我无法弄清楚,因为代码与我的不同。

目前我没有任何线路试图从 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;
?>

这是我需要的 XML 文件中的信息:

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

谢谢!

我希望 PHP 文件显示媒体文件。

【问题讨论】:

  • 您的url 代码如何获得php
  • 这是我想知道的事情
  • 试试这个:$url= $xml-&gt;channel-&gt;item[$i]-&gt;url;

标签: php xml rss


【解决方案1】:

您能告诉我们更多信息吗? 运行 simplexml_load_file 后,'$xml' 的值是多少? (你得到正确的数据了吗?)

你收到了什么错误信息?

编辑 - 根据您的评论

尝试使用

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

children 中的“媒体”是“内容”元素的命名空间。 而布尔值“true”变量告诉解析器将“媒体”引用为命名空间

【讨论】:

  • 感谢您的回复。我删除了 xml 文件和媒体网址。但是使用我提供的代码,一切正常。它获取标题(添加链接)、描述、发布日期和作者姓名。这段代码我没有收到任何错误。我只是不知道如何定义媒体:从xml文件到php和html的内容。
  • 所以如果我理解正确,该行应该如下$content = $xml-&gt;channel-&gt;item[$i]-&gt;children('media', true)-&gt;content-&gt;attributes(); 并且用HTML显示它应该是$html .= "$content"; 这会给我一个错误“警告:SimpleXMLElement::__toString() : 节点不再存在于 /Users/lrakremmis/Documents/Projects/RSS/index.php 第 22 行"
  • 这是因为$content 是一个对象,而您正试图将其用作字符串(因此调用魔术函数__toString())。试试看$content的内容,看看能不能自己解决:)如果需要更多帮助请告诉我
猜你喜欢
  • 1970-01-01
  • 2020-12-09
  • 2018-04-20
  • 2015-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-25
相关资源
最近更新 更多