简体   繁体   English

PHP RSS Feed未显示

[英]PHP RSS Feed not showing

New to PHP and was just playing around with RSS feeds. PHP的新手,只是在使用RSS feed。

I'm currently just trying to display the RSS feed from here: http://www.polygon.com/rss/index.xml . 我目前正试图从此处显示RSS feed: http://www.polygon.com/rss/index.xml : http://www.polygon.com/rss/index.xml

Bbut I think my code is broken somewhere and was hoping someone could shed some light on the issue. 但是我认为我的代码在某个地方坏了,希望有人可以对此问题有所了解。

This is the function I am using: 这是我正在使用的功能:

<?php

function fetch_news(){
    $data = file_get_contents('http://www.polygon.com/rss/index.xml');
    $data = simplexml_load_string($data);

    $articles = array();

    foreach ($data->channel->item as $item){
        $articles[] = array(
            'title'     => (string)$item->title,
            'content'   => (string)$item->content,
            'href'      => (string)$item->href,
            'published' => (string)$item->published,
            );
    }

    print_r($articles);
}

?>

When loading the page no content is displaying :( All I get is this: 当加载页面时,没有内容显示:(我得到的是这样的:

Array ( ) 数组()

Any Ideas on what I am doing wrong? 对我做错的任何想法吗? I'm guessing it has something to do with that foreach statement. 我猜它与该foreach语句有关。

Thanks for any help :) 谢谢你的帮助 :)

First off, there is no ->channel->item inside $data . 首先, $data没有->channel->item It's ->entry . ->entry

Second, you can gather the results in the container and then return the values gathered. 其次,您可以在容器中收集结果,然后返回收集的值。 Then invoke the function: 然后调用该函数:

function fetch_news(){
    $articles = array();
    $data = simplexml_load_file('http://www.polygon.com/rss/index.xml');
    foreach ($data->entry as $entry){
        $articles[] = array(
            'title'     => (string) $entry->title,
            'content'   => (string) $entry->content,
            'href'      => (string) $entry->href,
            'published' => (string) $entry->published,
        );
    }

    return $articles;
}

$news = fetch_news();
echo '<pre>';
print_r($news);

Sample Output 样本输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM