You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除JSON响应中的@attributes键,获取XML media:content的url

问题描述

处理RSS XML(例如纽约时报的RSS源)时,想要提取media:content属性中的url值,但当前代码返回的JSON响应中带有@attributes键,只需要单独的url字段。相关代码如下:

private $rss_urls = array(
    'BBC' => ['url' => 'http://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml'],
    'NPR' => ['url' => 'https://www.npr.org/rss/rss.php?id=1001'],
    'NYT' => ['url' => 'https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml', 'image_tag' => 'media:content'],
);
function fetch_rss(&$feed, $url, $source_name, $image_tag = null)
{
    $xml = simplexml_load_file($url);
    unset($xml->attributes()->domain);
    if (!$xml)
        return false;

    foreach ($xml->channel->xpath('//item') as $index => $xml_item) {
        $feed_item = false;
        $feed_item['title'] = strip_tags(trim($xml_item->title));
        $feed_item['description'] = strip_tags(trim($xml_item->description));
        $feed_item['link'] = strip_tags(trim($xml_item->link));
        $feed_item['date'] = strtotime($xml_item->pubDate);
        $feed_item['source'] = $source_name;
        if ($image_tag) {
            $feed_item['image'] = $xml->channel->item[$index]->children('media', True)->content->attributes();
        }
        $current_date = strtotime('today');
        if (date('Y-m-d', $feed_item['date']) === date('Y-m-d', $current_date)) {
            $feed[] = $feed_item;
        }
    }
    return $feed;
}
解决方法

问题根源在于直接将attributes()返回的SimpleXMLElement对象赋值给了$feed_item['image'],该对象转JSON时会保留@attributes结构。要获取纯url值,可按以下方式修改:

  • 直接提取并转换为字符串
    调用attributes()后,直接指定url属性,同时强制转换为string类型,避免对象残留的结构:

    $feed_item['image'] = (string)$xml->channel->item[$index]->children('media', true)->content->attributes()['url'];
    
  • 优化循环逻辑(推荐)
    循环中已经拿到了$xml_item,无需再通过$xml->channel->item[$index]重复获取元素,直接使用当前$xml_item更高效:

    if ($image_tag) {
        $media_content = $xml_item->children('media', true)->content;
        $feed_item['image'] = (string)$media_content->attributes()->url;
    }
    
修改后的完整代码
private $rss_urls = array(
    'BBC' => ['url' => 'http://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml'],
    'NPR' => ['url' => 'https://www.npr.org/rss/rss.php?id=1001'],
    'NYT' => ['url' => 'https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml', 'image_tag' => 'media:content'],
);
function fetch_rss(&$feed, $url, $source_name, $image_tag = null)
{
    $xml = simplexml_load_file($url);
    unset($xml->attributes()->domain);
    if (!$xml)
        return false;

    foreach ($xml->channel->xpath('//item') as $xml_item) {
        $feed_item = [];
        $feed_item['title'] = strip_tags(trim($xml_item->title));
        $feed_item['description'] = strip_tags(trim($xml_item->description));
        $feed_item['link'] = strip_tags(trim($xml_item->link));
        $feed_item['date'] = strtotime($xml_item->pubDate);
        $feed_item['source'] = $source_name;
        if ($image_tag) {
            $media_content = $xml_item->children('media', true)->content;
            $feed_item['image'] = (string)$media_content->attributes()->url;
        }
        $current_date = strtotime('today');
        if (date('Y-m-d', $feed_item['date']) === date('Y-m-d', $current_date)) {
            $feed[] = $feed_item;
        }
    }
    return $feed;
}

内容的提问来源于stack exchange,提问作者lucasbbs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 08:38:24