I am trying to convert an rss feed to JSON Object, I had some success in converting to JSON object but structure is little off.
Below is the output what I am getting.
{
"title": "Woot",
"description": "One Day, One Deal",
"link": "http:\/\/www.woot.com\/?utm_source=version1&utm_medium=rss&utm_campaign=api.woot.com",
"item": [
{
"title": "Bounty Hunter Snooper II Metal Detector"
},
{
"price": "$64.99"
},
{
"type": "New"
},
{
"title": "GIV Mobile Phones with One Month Unlimited Service"
},
{
"price": "$249.99"
},
{
"type": "Refurbished"
}
]
}
Output what i want
{
"title": "Woot",
"description": "One Day, One Deal",
"link": "http:\/\/www.woot.com\/?utm_source=version1&utm_medium=rss&utm_campaign=api.woot.com",
"item": [
{
"title": "Bounty Hunter Snooper II Metal Detector",
"price": "$64.99",
"type": "New"
},
{
"title": "GIV Mobile Phones with One Month Unlimited Service",
"price": "$249.99",
"type": "Refurbished"
}
]
}
code which i am using
<?php
header('Content-Type: application/json');
$feed = new DOMDocument();
$feed->load('RSS Feed Url');
$json = array();
$json['title'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
$json['description'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
$json['link'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('link')->item(0)->firstChild->nodeValue;
$items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item');
$json['item'] = array();
$i = 0;
foreach($items as $item) {
$title = $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
$description = $item->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
$purchaseurl = $item->getElementsByTagName('purchaseurl')->item(0)->firstChild->nodeValue;
$standardimage = $item->getElementsByTagName('standardimage')->item(0)->firstChild->nodeValue;
$shipping = $item->getElementsByTagName('shipping')->item(0)->firstChild->nodeValue;
$price = $item->getElementsByTagName('price')->item(0)->firstChild->nodeValue;
$condition = $item->getElementsByTagName('condition')->item(0)->firstChild->nodeValue;
$guid = $item->getElementsByTagName('guid')->item(0)->firstChild->nodeValue;
$json['item'][$i++]['title'] = $title;
$json['item'][$i++]['description'] = $description;
$json['item'][$i++]['purchaseurl'] = $purchaseurl;
$json['item'][$i++]['image'] = $standardimage;
$json['item'][$i++]['shipping'] = $shipping;
$json['item'][$i++]['price'] = $price;
$json['item'][$i++]['type'] = $condition;
$json['item'][$i++]['guid'] = $guid;
}
echo json_encode($json);
?>
Let me know your thoughts, Thanks in Advance!