RSS Feed fetch作为JSON对象

时间:2015-06-04 14:31:34

标签: php json symfony rss

这是我的名为testClient的PHP文件

  **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;
}**

这是我想要查看结果的控制器类

**

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;
    }

**

如果我想从标题/描述/链接等RSS提要中获取数据,我就会得到这些数据。但我如何将所有这些数据作为JSON对象获取。因为在rss Feed中数据正在保存为项目。

这是来自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>

**

提前致谢...

1 个答案:

答案 0 :(得分:0)

您在第一步中将HTML添加到数据中。你应该在最后做到这一点。最好的选择是使用Twig模板。

然后将XML解析为JSON非常简单:

'_item01'