I want to get a Media:description from NYT external (RSS) XML file.
The following code works:
<?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']);
}
?>
This results in:
How can I continue?
Also I kind of know to get description with another method. But using two methods I will not keep the (item)description in the same Array. Code to extract description could be:
Following code:
<!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>
RESULT IN:
-- Edit 2:
Just was thinking about limit barrier too. And I know this code works :
<?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(' & ', ' & ', $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>';
?>
But I don't know really how to use it in my current method.