RSS Feed和图像提取深入

时间:2010-09-25 12:22:49

标签: php image rss magpie

我花了很多时间来解决这个问题,这就是我所拥有的。基本上我试图从rss feed中提取图像。我使用magpie来处理Feed,如下所示..这个片段在一个类

function getImagesUrl($str) {
    $a = array();
    $pos = 0;
    $topos;
    $init = 1;

    while($init) {
        $pos = strpos($str, "img",  $pos);
        if($pos != FALSE) {
            $topos = strpos($str, ">", $pos);
            $imagetag = substr($str, $pos, ($topos - $pos));
            $url = $this->getImageUrl($imagetag);
            $pos = $topos;
            array_push($a, $url);
        }
        else {
            $init = 0;
        }
    }
    return $a;
}


/*
 * get the full url inside src atribute in <img>
*/
function getImageUrl($image) {
    $p = strpos($image, "src=", 0);
    $p+= 5; // remove o src="
    $tp = strpos($image, '" ', $p);
    $str = substr($image, $p, ($tp - $p));
    return $str;
}                

使用上述功能...我用这种方式调用它...到目前为止,这将输出我稍后将粘贴的数据

            @$rss = fetch_rss($rsso->url);
            if (@$rss)
                {
                $items=$rss->items;
                  foreach ($items as $item ) 
                    {
                    if (isset($item['title'])&&isset($item['description']))
                        {
                    $hash=md5($this->es($item['title']).$this->es($item['description']));
                     $content = $item['content'];
                    foreach($content as $c) {
                        // get the images on content
                        $arr = $this->getImagesUrl($c);
                        print_r($arr);
                        }

这是一个输出

的例子
 1. Array ( [0] =>
    http://api.tweetmeme.com/imagebutton.gif?url=http://mashable.com/2010/09/25/trailmeme/
    [1] =>
    http://cdn.mashable.com/wp-content/plugins/wp-digg-this/i/gbuzz-feed.png
    [2] =>
    http://mashable.com/wp-content/plugins/wp-digg-this/i/fb.jpg
    [3] =>
    http://mashable.com/wp-content/plugins/wp-digg-this/i/diggme.png
    [4] =>
    http://ec.mashable.com/wp-content/uploads/2009/01/bizspark2.gif
    [5] =>
    http://cdn.mashable.com/wp-content/uploads/2010/09/web.png
    [6] =>
    http://mashable.com/wp-content/uploads/2010/09/Screen-shot-2010-09-24-at-10.51.26-PM.png
    [7] =>
    http://cdn.mashable.com/wp-content/uploads/2009/02/bizspark.jpg
    [8] =>
    http://feedads.g.doubleclick.net/~at/lxx00QTjYBaYojpnpnTa6MXUmh4/0/di
    [9] => [10] =>
    http://feedads.g.doubleclick.net/~at/lxx00QTjYBaYojpnpnTa6MXUmh4/1/di
    [11] => [12] =>
    http://feeds.feedburner.com/~ff/Mashable?i=0N_mvMwPHYk:j5Pmi_N-JQ8:D7DqB2pKExk [13] => [14] =>
    http://feeds.feedburner.com/~ff/Mashable?i=0N_mvMwPHYk:j5Pmi_N-JQ8:V_sGLiPBpWU [15] => [16] =>
    http://feeds.feedburner.com/~ff/Mashable?i=0N_mvMwPHYk:j5Pmi_N-JQ8:F7zBnMyn0Lo [17] => [18] =>
    http://feeds.feedburner.com/~ff/Mashable?d=qj6IDK7rITs
    [19] => [20] =>
    http://feeds.feedburner.com/~ff/Mashable?d=_e0tkf89iUM
    [21] => [22] =>
    http://feeds.feedburner.com/~ff/Mashable?i=0N_mvMwPHYk:j5Pmi_N-JQ8:gIN9vFwOqvQ [23] => [24] =>
    http://feeds.feedburner.com/~ff/Mashable?d=yIl2AUoC8zA
    [25] => [26] =>
    http://feeds.feedburner.com/~ff/Mashable?d=P0ZAIrC63Ok
    [27] => [28] =>
    http://feeds.feedburner.com/~ff/Mashable?d=I9og5sOYxJI
    [29] => [30] =>
    http://feeds.feedburner.com/~ff/Mashable?d=CC-BsrAYo0A
    [31] => [32] =>
    http://feeds.feedburner.com/~ff/Mashable?i=0N_mvMwPHYk:j5Pmi_N-JQ8:_cyp7NeR2Rw [33] => [34] =>
    http://feeds.feedburner.com/~r/Mashable/~4/0N_mvMwPHYk
    )

有没有办法可以过滤出正确的图片网址?例如....我想删除没有“jpg,png,gif”等扩展名的网址。其次,我想废弃网址,例如bizspark,digg,facebook,tweet,twitter等。任何人发现任何更简单的方法吗?请帮帮我

1 个答案:

答案 0 :(得分:0)

我在这里发布了你的相关问题的答案: Pulling Images from rss/atom feeds using magpie rss

要将该答案应用于上述代码,请首先根据我之前的答案对rss_parse.inc进行更改。然后你可以通过Magpie访问图像网址(而不是必须编写任何额外的功能),例如。

// Your code
@$rss = fetch_rss($rsso->url);
if (@$rss)
{
   $items=$rss->items;
   foreach ($items as $item ) 
   {
      if (isset($item['title'])&&isset($item['description']))
      {
         // START MY EDIT
         if (isset($item['enclosure_type']) && isset($item['enclosure_url'])){
            switch ($item['enclosure_type']){
               case "image/gif":
               case "image/jpeg":
               case "image/png":
                   $image_url=$item['enclosure_url'];
                   $image_length=$item['enclosure_length'];
                   break;
            }
         }
         //END MY EDIT
       }
   }
}

就是这样!你只需使用$ image_url var来显示你的图像(当然是img标签: - )

我只检查了上面代码中的jpg,gif和png图像,因为它们是最受欢迎的,但如果需要,可以在交换机中添加其他mime类型。请注意,机箱类型是由RSS源的创建者设置的,而不是从文件中读取,因此可能不准确。您可能希望在图像文件本身上使用exif_imagetype()以确保它实际上是图像。

希望如果不是太晚,这会有所帮助!