Search code examples
phpjsonrss

How to convert RSS feed to JSON in php


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!


Solution

  • Test with this

    try this way for the item iteration

    foreach($items as $item) {
    
      $json['item'][] = array("title"=>$title,"price"=>$price,"description"=>$description)
    }
    echo json_encode($json)
    

    The reason you got that is because you had given $i++for every array index if you had just given $json['item'][$i] and $i=$i+1 at the end you would have got desired output

    So also this will work

    $i=0;
     foreach($items as $item) {
    
      $json['item'][$i] = array("title"=>$title,"price"=>$price,"description"=>$description)
      $i=$i+1; 
     }
    echo json_encode($json)
    

    Third approach

    $json['item'][$i++]['guid'] = $guid;
    

    Apply $i++ only to the last element and $i to the rest becuase $i++ is post increment