首页 文章

在网站上加载RSS在本地工作(MAMP)但不能远程使用PHP $ rss = new DOMDocument();和$ rss-> load('http://url/rss.xml');? [重复]

提问于
浏览
1

这个问题在这里已有答案:

我正在尝试将RSS源加载到我的网站中,我找到了一个PHP脚本来执行此操作并将一些可设置样式的标记应用于内容 .

下面是获取RSS提要的原始脚本,并在我的网站上将其作为可设置样式的HTML输出(这在本地工作,RSS项目很好地放在我的网站上的本地测试服务器上)当我将它上传到我的网络服务器时,它将无法获得RSS项目,它只显示RSS内容所在的空容器 .

这是 rss-speech.php 脚本:

$rss = new DOMDocument();
$rss->load('http://www.whitehouse.gov/podcast/audio/speeches/rss.xml');
$feed = array();

foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array (
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
    );

    array_push($feed, $item);
}

$limit = 34;

for($x=0;$x<$limit;$x++) {
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link = $feed[$x]['link'];
    $description = $feed[$x]['desc'];
    $date = date('l F d, Y', strtotime($feed[$x]['date']));

    echo '<div class="rssitem">';
    echo '<p><a href="'.$link.'" title="'.$title.'">'.$title.'</a>
'; echo '<small><em>Posted on '.$date.'</em></small></p>'; echo '<p style=" ">'.$description.'</p>'; echo '</div>'; }

当我在我的本地testserver(MAMP)上测试时,这似乎工作正常,但是当我将脚本FTP到我的远程web服务器时,它似乎没有加载feed(其他一切都有效,因为我可以看到脚本试图制作“发布于”行,内容刚刚丢失 .

我认为它可能与跨域feed解析有关,所以我试图像这样制作一个php代理:

rss-proxy.php

<?php
header ("Content-Type:text/xml");

echo file_get_contents('http://www.whitehouse.gov/podcast/audio/speeches/rss.xml');
?>

如果我运行它,它将在浏览器中显示如下内容:

en clean Public Domain白宫在这个全面的播客中跟上奥巴马总统的所有言论,市政厅和新闻发布会 . 这篇文章有时会包括副总统拜登和第一夫人米歇尔奥巴马等其他校长的言论 . 在这个全面的播客中了解奥巴马总统的所有言论,市政厅和新闻发布会 . 这个饲料偶尔[等...]

我正在尝试解析的XML示例:

<channel>
    <title>White House Speeches (Audio)</title>
    <link>http://www.whitehouse.gov/podcast/audio/press-briefings/rss.xml
        <description></description>
        <language>en</language>
        <atom:link href="http://www.whitehouse.gov/podcast/audio/speeches/rss.xml" rel="self" 
           type="application/rss+xml">
           <itunes:explicit>clean</itunes:explicit>
           <copyright>Public Domain</copyright>
           <itunes:author>The White House</itunes:author>
           <itunes:image href="http://www.whitehouse.gov/sites/default/files/imagecache/podcast_detail
               /podcasts/audio/speeches_audio.jpg">
               <itunes:subtitle>Keep up with all of President Obama's remarks, town halls, and press 
                   conferences in this comprehensive podcast.  This feed will occasionally include remarks from 
                   other principals like Vice President Biden and First Lady Michelle Obama.</itunes:subtitle>
                   <itunes:summary>Keep up with all of President Obama's remarks, town halls, and press 
                       conferences in this comprehensive podcast.  This feed will occasionally include remarks from    
                       other principals like Vice President Biden and First Lady Michelle Obama.</itunes:summary>
                       <itunes:category text="Government &amp; Organizations">
                        <item>
                           <title>Young African Leaders Initiative Town Hall</title>
                           <link>http://www.whitehouse.gov/photos-and-video/video/2013/06/29/young-african-leaders-
                               initiative-town-hall
    <description><!--[CDATA[At the University of Johannesburg - Soweto, President Obama discusses 
       youth empowerment and leadership with young African leaders in a town hall meeting.]]-->
    </description>
    <enclosure url="http://www.whitehouse.gov/videos/2013/June/062913_UniversityofJohannesburg.mp3" type="audio/mpeg">
       <category domain="http://www.whitehouse.gov/taxonomy/term/16">The President</category>
       <category domain="http://www.whitehouse.gov/admin/category/issue-tag/foreign-policy">
           Foreign Policy</category>
           <category domain="http://www.whitehouse.gov/taxonomy/term/9">Speeches &amp; Events</category>
           <itunes:keywords>Foreign Policy</itunes:keywords>
           <itunes:author>The White House</itunes:author>
           <itunes:summary>
               <itunes:subtitle>At the University of Johannesburg - Soweto, President Obama discusses 
                   youth empowerment and leadership with young African leaders in a town hall meeting.         
               </itunes:subtitle>
               <itunes:explicit>clean</itunes:explicit>
               <pubdate>Sat, 29 Jun 2013 21:26:37 +0000</pubdate>
               <dc:creator>The White House</dc:creator>
               <guid ispermalink="false">223196 at http://www.whitehouse.gov</guid>
           </itunes:summary>
       </enclosure>
   </item>
</channel>

然后让 rss-speech.php 脚本执行:

$rss->load('rss-proxy.php');

这就是它出错的地方,新的DOM文档似乎从 rss-proxy.php 脚本中得不到任何东西,即使我刚刚确认代理正确获取XML源代码 .

我也尝试过这个:http://benalman.com/projects/php-simple-proxy/

我甚至尝试使用以下命令将RSS提要缓存到缓存目录中的XML文件:http://www.javascriptkit.com/dhtmltutors/ajaxticker/ajaxticker2.shtml

这似乎在本地正确创建XML文件,但远程它创建一个0kb空XML文件 . 我已经通过FTP检查了权限,以便对所有人进行读/写操作 . 此外,如果我尝试将此文件中的内容发送到 rss-speech.php 脚本,则它不起作用(本地或远程) .

似乎也提供了很多解析错误,并且它只生成0kb空缓存的RSS文档 .

我尝试的任何东西似乎都没有用,它只是不会从RSS提要中获取内容 .

关于如何让这个工作的任何想法?

1 回答

  • 1

    我解决了我的问题!

    我使用cURL而不是file_get_contents .

    我发现问题是由于我的托管服务器上禁用了allow_url_fopen . 然后我找到了一个关于如何使用cURL来阅读提要的精彩教程 .

    http://ditio.net/2008/06/19/using-php-curl-to-read-rss-feed-xml/

    顺便感谢你的帮助!

相关问题