【问题标题】:RSS Feed fetch as a JSON objectRSS 提要获取为 JSON 对象
【发布时间】:2015-08-19 05:36:38
【问题描述】:

这是我的名为 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 提要中,数据被保存为一个项目。

这是来自 RSS 提要的示例“项目”数据 ---

**

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

**

提前致谢...

【问题讨论】:

    标签: php json symfony rss


    【解决方案1】:

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

    然后将 XML 解析为 JSON 就很简单了:

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

    【讨论】:

      猜你喜欢
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多