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

How to retrieve a Media Description from an RSS file in PHP?

我想从 NYT 外部 (RSS) XML 文件中获取 Media: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;
        }
    }

    $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']);
}
?>

这导致:

我该如何继续?

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

代码如下:

<!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>

结果:

-- 编辑 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>';

?>

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

在循环中尝试下面的代码,

$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

您可以使用 $description = $entry->children('media', true)->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++; 

  } 
  } 
    ?>