I am trying to parse rss feeds from various sources,one of such source is this:http://feeds.feedburner.com/DiscoveryNews-Top-Stories
But this source is giving me some weird json data like this:
"item": [
{
"title": [
"Snazzy Science Photos of the Week (August 10-16)",
{
"type": "html",
"content": "Snazzy Science Photos of the Week (August 10-16)"
}
],
"description": [
"Glowing rabbits, treasure-hunting badgers and a case of mistaken UFO identity help round out this week's photos.<img src=\"http://feeds.feedburner.com/~r/DiscoveryNews-Top-Stories/~4/S6Urfvdw2DQ\" height=\"1\" width=\"1\"/>",
{
"type": "html",
"content": "Glowing rabbits, treasure-hunting badgers and a case of mistaken UFO identity help round out this week's photos."
}
],
Currently,I'm using the following code to get titles of the posts:
if(isset($jit->title->content)){
$title = $decoded_json->query->results->item->title->content;
}else{
$title = $decoded_json->query->results->item->title;
}
But the above code fails when I try to parse Discovery news feed.Please help?
[EDIT]: I'm using YQL to get the equivalent JSON from the source.This is the link
It is packing up the elements as an array:
"title": [
"No Battery Required for This Wireless Device",
{
"type": "html",
"content": "No Battery Required for This Wireless Device"
}
],
You can read the first element like this:
<?php
$url = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20feed%20where%20url=%22http://feeds.feedburner.com/DiscoveryNews-Top-Stories%22&format=json&diagnostics=true&callback=cbfunc';
$data = substr(file_get_contents($url), 7, -2);
$json = json_decode($data);
foreach ($json->query->results->item as $item)
{
echo "Title: ", $item->title[0], "\nDescription: ", $item->description[0], "\n";
echo "==================================================\n\n";
}
Or using SimpleXML library:
$url = 'http://feeds.feedburner.com/DiscoveryNews-Top-Stories?format=xml';
$xml = simplexml_load_string(file_get_contents($url));
foreach ($xml->channel->item as $item)
{
echo "Title: ", $item->title, "\nDescription: ", $item->description, "\n";
echo "==================================================\n\n";
}