如何从PHP中的RSS文件中检索媒体描述?

时间:2017-04-17 10:16:00

标签: php xml image rss media

我想从NYT外部(RSS)XML文件获取媒体:描述。

以下代码有效:

<?php
$url = "http://rss.nytimes.com/services/xml/rss/nyt/Sports.xml"; // xmld.xml contains above data
$feeds = file_get_contents($url);
$rss = simplexml_load_string($feeds);
$items = [];
foreach($rss->channel->item as $entry) {
    $image = '';
    $image = 'N/A';
    foreach ($entry->children('media', true) as $k => $v) {
        $attributes = $v->attributes();
            if (count($attributes) == 0) {
            continue;
        } else {
            $image = $attributes->url;
        }
    }

    $items[] = [
        'link' => $entry->link,
        'title' => $entry->title,
        'image' => $image,
    ];

}

//print_r($items);

foreach ($items as $item) {
    printf('<img src="%s">', $item['image']);
    printf('<a href="%s">%s</a>', $item['link'], $item['title']);
}
?>

这导致:

image and text

我该如何继续?

我也知道用另一种方法来描述。但是使用两种方法我不会将(item)描述保留在同一个Array中。提取描述的代码可以是:

以下代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>How to Parse XML with SimpleXML and PHP</title>
</head>
<body>
<?php
$url = 'http://rss.nytimes.com/services/xml/rss/nyt/Sports.xml';
$xml = simplexml_load_file($url) or die("Can't connect to URL");

?><pre><?php //print_r($xml); ?></pre><?php

foreach ($xml->channel->item as $item) {
    printf('<li><a href="%s">%s</a></li><br>%s', $item->link, $item->title, $item->description);
}
?>  
</body>
</html>

结果:

Link and Description but no image - as simplexml_load_file doesn't handle media tags

- 编辑2:

也正在考虑限制障碍。 我知道这段代码有效:

<?php

$rss = new DOMDocument();
$rss->load('http://careers.pageuppeople.com/671/cw/en-us/rss');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
                $item = array ( 
                                'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
                                'desc' => $node->getElementsByTagNameNS("http://pageuppeople.com/","description")->item(0)->nodeValue,
                                'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
                                'pubDate' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
                                'closeDate' => $node->getElementsByTagName('closingDate')->item(0)->nodeValue,
                                'field_city' => $node->getElementsByTagName('location')->item(0)->nodeValue,
                                );
                array_push($feed, $item);
}
$limit = 50;
echo '<?xml/>';
for($x=0;$x<$limit;$x++) {
                echo '<item>';
                $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
                $link = $feed[$x]['link'];
                $description = $feed[$x]['desc'];
                $field_city = $feed[$x]['field_city'];
                $pubDate = date('Y: m: d', strtotime($feed[$x]['pubDate']));
                $closeDate = date('Y: m: d', strtotime($feed[$x]['closeDate']));
                echo '<title>'.$title.'</title>';
                echo '<pubDate>'.$pubDate.'</pubDate>';
                echo '<closeDate> '.$closeDate.'</closeDate>';
                echo '<link>'.$link.'</link>';
                echo '<field_city>'.$field_city.'</field_city>';
                echo '<body>'.$description.'</body>';
                echo '<field_how_to_apply><strong>UNICEF is committed to diversity and inclusion within its workforce, and encourages qualified female and male candidates from all national, religious and ethnic backgrounds, including persons living with disabilities, to apply to become a part of our organization.<br><br>To apply click on the link below.</strong><br><br>'.$link.'</field_how_to_apply>';
                echo '</item>';

}

echo '</channel></rss>';

?>

但我不知道如何在我目前的方法中使用它。

2 个答案:

答案 0 :(得分:1)

您可以使用获取说明 $ description = $ entry-&gt; children(&#39; media&#39;,true) - &gt; description;

 <?php
    $url = "http://rss.nytimes.com/services/xml/rss/nyt/Sports.xml"; // xmld.xml contains above data
    $feeds = file_get_contents($url);
    $rss = simplexml_load_string($feeds);

    $items = [];

    foreach($rss->channel->item as $entry) {
        $image = '';
        $image = 'N/A';
        foreach ($entry->children('media', true) as $k => $v) {
            $attributes = $v->attributes();

            if (count($attributes) == 0) {
                continue;
            } else {
                $image = $attributes->url;
            }
        $content_data = (string)$entry->children("media", true)->description;
        }


        $items[] = [
            'link' => $entry->link,
            'title' => $entry->title,
            'image' => $image,
            'Desc' =>$content_data,

        ];

    }

    //print_r($items);

       $i=0; 
foreach ($items as $item) {
 if ($i < 3) {

  printf('<img src="%s">', $item['image']);
  printf('<a href="%s">%s</a>', $item['link'], $item['title']); printf('<p>%s</p>', $item['Desc']);
   $i++; 

  } 
  } 
    ?>

答案 1 :(得分:0)

在循环中尝试以下代码,

$description = $entry->children('media', true)->description;

完整代码,

foreach($rss->channel->item as $entry) {
    $image = '';
    $image = 'N/A';
    foreach ($entry->children('media', true) as $k => $v) {
        $attributes = $v->attributes();
            if (count($attributes) == 0) {
            continue;
        } else {
            $image = $attributes->url;
        }
    }
    $items[] = [
        'link' => $entry->link,'title' => $entry->title,
        'image' => $image,'description'=>$entry->children('media', true)->description
    ];
}

foreach ($items as $item) {
    printf('<img src="%s">', $item['image']);
    printf('<a href="%s">%s</a>', $item['link'], $item['title']);
    echo '<p>'.$item['description'].'</p>';
}

<强> PHPFiddle