RSS Feed 提取为 JSON 对象

RSS Feed fetch as a JSON object

这是我的 PHP 文件,名为 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;
}**

这是我的控制器class,我想在其中查看结果

**

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

**

如果我想从 title/description/link 之类的 RSS 提要中获取数据,我正在获取这些数据。但是我如何才能将所有这些数据作为 JSON 对象。因为在 rss 提要中,数据保存为一个项目。

这是来自 RSS 提要的示例 "item" 数据 ---

**

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

**

提前致谢...

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

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

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