【问题标题】:Can SimplePie grab images from feedsSimplePie 可以从提要中抓取图像吗
【发布时间】:2012-03-09 03:37:05
【问题描述】:

我正在使用这个 simplepie,并且我已将文件上传到我的主机。除了一件事,一切似乎都运行良好。我从中获取提要的博客中有图像,当我使用 simplepie 查看提要时,图像不显示。当我用 simplepie 查看博客时,有没有办法让图像显示出来?

好的,对不起,我是新手。我只是直接使用网站上的代码来尝试查看博客。我将代码放在此处的底部。所以是的,就像我说的那样,我只是想让图像显示在我正在阅读的博客上。除此以外,一切都很好。

--- 标题信息 ---

<?php

// Make sure SimplePie is included. You may need to change this to match the location of simplepie.inc.
require_once('simplepie.inc');

// We'll process this feed with all of the default options.
$feed = new SimplePie();

// Set the feed to process.
$feed->set_feed_url('http://wordpress.homickdesign.com/feed/');

// Run SimplePie.
$feed->init();

// This makes sure that the content is sent to the browser as text/html and the UTF-8 character set (since we didn't change it).
$feed->handle_content_type();

// Let's begin our XHTML webpage code.  The DOCTYPE is supposed to be the very first thing, so we'll keep it on the same line as the closing-PHP tag.
?>

--- 代码---

<h2><a href="<?php echo $feed->get_permalink(); ?>"><?php echo $feed->get_description(); ?></a><br />
      <span>Latest news from Robert Homick</span></h2>       

        <?php
$c=1;   
foreach ($feed->get_items() as $item):
if($c <= 3){
?>

        <p><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a><br />
        <?php echo $item->get_description(); ?><br />
        <span>Posted on <?php echo $item->get_date('j F Y | g:i a'); ?></span><br />
        <?php $c++;?>


<?php } endforeach;  ?>

【问题讨论】:

  • 除非您发布源代码,否则我们无法帮助您。是的,SimplePie 获取媒体元素,但有些类型可能很棘手。
  • 好的,我添加了代码,很抱歉我是新手。
  • 你应该接受一个 Muskies 的回答

标签: php import rss feed simplepie


【解决方案1】:

是的,您可以调用 get_content() 并将整个帖子或包含在提要中的帖子部分输出到屏幕上,包括所有 HTML 标记。如果你想拉出帖子中的第一张图片,你必须做更多的工作,这就是我一直在做的。

$htmlDOM = new simple_html_dom();
$htmlDOM->load($item->get_content());
$image = $htmlDOM->find('img', 0); 
$html .= '<img src="thumbnail.php?file=' . $image->src . '&maxw=300&maxh=300" border="0" />';

我当前的小项目中的那个 sn-p,使用 simple_html_dom 和我从 Internet 上下载的另一个脚本来动态调整图像大小。如果您只想显示图像,根本不调整它的大小 $image->src 是 URL。我认为这样的功能将来可能会添加到 SimplePie 或 SimplePie 的附加组件中,但与此同时,显示来自提要的项目的第一张图像需要四行代码。

【讨论】:

  • 我似乎无法让$htmlDOM = new simple_html_dom(); 返回任何东西(我包括github.com/samacs/simple_html_dom/blob/master/…)。
  • 我怀疑我正在运行最新的代码,但如果 $htmlDOM 没有返回任何内容,则可能您的提要为空或不包含 HTML。在广泛使用 RSS 提要后,它们通常不遵循标准,如果你有垃圾进入,你就会有垃圾出来。简单的 HTML DOM 和 SimplePie 无疑会进行一些错误检查,但有些文本可能会让他们感到悲伤,我在上面编写了自己的错误条件处理代码,但事情仍在不断变化,Pinterest 和 YouTube 都做出了改变。 YouTube 有一个我必须学习的全新 API...
  • 该代码 sn-p 已从我的主页上删除,因此代码仍在运行,就像我说的,YouTube 不再支持某些旧的 API 代码,所以我必须在我之后进一步调查考试,但是该代码确实有效,并且可能仍在我的个人网站 www.muschamp.ca 上有效,您在尝试什么提要,它实际上是 RSS 以及 RSS 标准的哪个版本,提要本身是否验证?
【解决方案2】:

This tutorial 将帮助您将图像嵌入到简单的饼图输出中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多