1
votes

This is my PHP file named testClient

  **public function getXML() {

        $xml = simplexml_load_file('https:/.............');
        $feed1 = '<h3>' . $xml->channel->title . '</h3>';
        foreach ($xml->channel->item as $item) {
            $feed1 .= '<h4><a href="' . $item->link . '">' . $item->title . "</a></h4>";
        }
        $xml = simplexml_load_file('https://rumble.com/rss.php?target=sprinklevideo');
        $feed2 = '<h3>' . $xml->channel->description . '</h3>';
        foreach ($xml->channel->item as $item) {
            $feed2 .= '<h4><a href="' . $item->link . '">' . $item->description . "</a></h4>";
        }


        $xml = simplexml_load_file('https://rumble.com/rss.php?target=sprinklevideo');
        $feed3 = '<h3>' . $xml->channel->link . '</h3>';
        foreach ($xml->channel->item as $item) {
            $feed3 .= '<h4><a href="' . $item->link . '">' . $item->link . "</a></h4>";
        } 
        return $feed1;
}**

This is my controller class where i want to viewing the result

**

use VideoBundle\VideoProviderClient\testClient as testClient;
    class MainController extends Controller
    {

   public function MainAction() {
        $zoo = new testClient();
        $val = $zoo->getXML();

        //dump 
        echo "<pre>";
        var_dump($val);       
        echo "</pre>";
        die();
        return new Response("$val");
    }
    public function setVal($val) {
        $this->item = json_encode($val);
        return $this;
    }
    public function getVal() {
        if ($this->item) {
            return json_decode($val);
        }
        return null;
    }

**

If i want to fetch data from RSS feed like title/description/link, i am getting those. But HOW CAN I GET ALL THOSE DATA AS A JSON OBJECT. Because in rss feed the data is saving as a item.

This is a sample "item" data from RSS feed ---

**

<item>
<title>Newlyweds stun guests with epic first dance</title>
<description>
your foot in time to the music!
</description>
<link>
https://zzx.com/v2zl27-bride-and-groom-perform-the-best-first-dance-ever-to-a-swingin-classic.html
</link>
<guid isPermaLink="false">media/8.5087</guid>
<pubDate>2015-06-03 12:49:16</pubDate>
<media:category scheme="http://search.yahoo.com/mrss/category_schema">viral</media:category>
<media:keywords>
viral videos, inspiring first wedding dance, songs for first wedding dance
</media:keywords>
<media:thumbnail url="https://rumble.com/rss/8-5019487.jpg" />
<media:player url="https://asdas.com/bin/8_8" height="426" width="491" />
<media:content url="https://i.rmbl.ws/s8/d99" type="video/mp4" />
</item>

**

Thanks in advance ...

1

1 Answers

0
votes

You are adding HTML to your data on the first step. You should do that at the end. And the best option would be with a Twig template.

Then parsing XML to a JSON is quite simple:

$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);