从rss feed解析图像

时间:2018-10-19 19:07:14

标签: php xml rss

这是我现在拥有的代码。我正在尝试获取该网站的图像,但该变量未返回任何内容,这意味着我一直在找几个小时都无法获取它

tolerance = 0.000000001;
count = 0;
c =(a+b)/2;
while abs(f(c))>tolerance
    count=count+1;
    if f(c) > 0
        b = c;
    else
        a = c;
    end
end
answer = c;

1 个答案:

答案 0 :(得分:0)

查看您提供的RSS提要,它似乎没有站点图像。因此,如果您想要网站的徽标,则可能必须静态链接该徽标。

如果您想获取帖子的图片,我们可以做到。这就是我要做的。

我创建了一个程序包来使xml解析变得轻而易举。您可以在这里找到它:https://github.com/mtownsend5512/xml-to-array

然后执行以下操作:

$xml = \Mtownsend\XmlToArray\XmlToArray::convert(file_get_contents('https://www.idownloadblog.com/tag/jailbreak/feed/'));

现在您有了一个很好的rss feed的php数组。

接下来,我们将创建一个辅助函数,以从帖子的正文中获取第一张图片。我们将其用作帖子的特色图片。

function getPostImage($content)
{
    $output = preg_match_all('/<img[^>]+src=[\'"]([^\'"]+)[\'"][^>]*>/i', $content, $matches);
    if (empty($matches[1][0])) {
        return 'http://yoursite.com/images/fallback-image.jpg';
    }
    return $matches[1][0];
}

如果帖子中没有图片,您将用后备图片的网址替换http://yoursite.com/images/fallback-image.jpg

现在,我们遍历帖子:

foreach ($xml['channel']['item'] as $post) {
    $title = $post['title']);
    $link = $post['link'];
    $description = $post['description'];
    $pubDate = $post['pubDate'];
    $image = getPostImage($post["content:encoded"]);
}
相关问题