【问题标题】:simplexml_load_file error in PHP 5.3PHP 5.3 中的 simplexml_load_file 错误
【发布时间】:2012-07-05 07:37:42
【问题描述】:

我正在使用以下代码读取 RSS 提要并输出结果。

function home_page_parser($feedURL) {
    $rss = simplexml_load_file($feedURL);
    $i = 0;
    
    echo  "<ul>";
    
    foreach ($rss->channel->item as $feedItem) {
        $i++;
        $myDate = ($feedItem->pubDate);
        $dateForm = explode(" ", $myDate);
        echo "<li class=\"rss-feed\"><a href=\"$feedItem->link\" title=\"$feedItem->title\" target=\"_blank\">".$feedItem->title."</a><br />" .$feedItem->pubDate. "</li>";
                
    if($i >= 3) break;
        
    echo "</ul>";
    }
}

它在我的 Rackspace Cloud 测试站点上运行 PHP 5.2 运行良好

在 Media Temple 的实时站点上运行 PHP 5.3,我收到以下错误:


警告:simplexml_load_file() [function.simplexml-load-file]:http:// 包装器在服务器配置中被 /.../html/includes/functions.php 第 39 行的 allow_url_fopen=0 禁用

警告:simplexml_load_file(http://www.chinaknowledge.com/Newswires/RSS_News/RSS_News.xml) [function.simplexml-load-file]:无法打开流:在第 39 行的 /.../html/includes/functions.php 中找不到合适的包装器

警告:simplexml_load_file() [function.simplexml-load-file]:I/O 警告:无法在 /.../html/includes/functions.php 第 39 行加载外部实体 "http://www.chinaknowledge.com/Newswires/RSS_News/RSS_News.xml"

警告:在第 44 行的 /.../html/includes/functions.php 中为 foreach() 提供的参数无效


第 39 行是这样的:

$rss = simplexml_load_file($feedURL);

我做错了什么或需要更改以在 5.3 上工作?

【问题讨论】:

    标签: simplexml php


    【解决方案1】:

    你不觉得这个错误描述得很清楚吗?

    http:// 包装器在服务器配置中被禁用 allow_url_fopen=0

    您将需要编辑 PHP 配置文件并更改配置 allow_url_fopen如果你不能直接尝试 ini_set()

    编辑:正如@evanmcd 在cmets 中指出的那样,这个配置只能在php.ini 中设置。 PHP documentation 参考。

    【讨论】:

    • 我可以将它添加到我的函数文件中吗?
    • 是的。尝试将其添加到顶部。但是ini_set功能在很多共享主机环境中也被禁用了。
    • 尝试放入函数文件,但没有运气。在 php.ini 文件里关掉了,打开了也没运气……我去和 Media Temple 聊一聊,看看能找到什么。
    • 错误信息有变化吗?您能否运行函数 phpinfo() 并检查它是否已打开
    • allow_url_fopen 不能通过 ini_set() 设置。见stackoverflow.com/questions/1804010/…
    【解决方案2】:

    这个错误是由于“http:// wrapper is disabled in the server configuration by allow_url_fopen=0”。为了避免这个问题,我们需要将此设置覆盖为 On 而不是 off。在我看来,大多数共享托管服务器 不允许您通过 ini_set('allow_url_fopen', 'on'); 进行这些设置;或 htaccess 覆盖。因此,我建议不要尝试这些方法来获取该提要,如下所示。使用 CURL 我们需要将提要 xml 的内容获取到一个变量中。然后处理我们的 simplexml 文件操作。

    例子

    $feed ='http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=mytwittername';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $feed);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // get the result of http query
    $output = curl_exec($ch);
    curl_close($ch);
    $xml = simplexml_load_file($output);
    

    【讨论】:

    • 对我来说,最后一行需要是$xml = simplexml_load_string($output)
    【解决方案3】:

    如果您不允许在服务器中编辑 php.ini,您可以使用 curl 获取 xml 并读取 xml Stirng,如下所示。

    function home_page_parser($feedURL) {
        $rss = simplexml_load_file(curlXML($feedURL);
        $i = 0;
    
        echo  "<ul>";
    
        foreach ($rss->channel->item as $feedItem) {
            $i++;
            $myDate = ($feedItem->pubDate);
            $dateForm = explode(" ", $myDate);
            echo "<li class=\"rss-feed\"><a href=\"$feedItem->link\" title=\"$feedItem->title\" target=\"_blank\">".$feedItem->title."</a><br />" .$feedItem->pubDate. "</li>";
    
        if($i >= 3) break;
    
        echo "</ul>";
        }
    }
    
    function curlXML($url){
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $url);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       // get the result of http query
       $output = curl_exec($ch);
       curl_close($ch);
    
       return $output;
    }
    

    【讨论】:

      【解决方案4】:
      ini_set("allow_url_fopen", 1);
      

      这将在 php.ini 文件中设置 allow url open = On 但您需要在 easyphp 或 xamp 或 wamp 或托管中重新启动 php。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-16
        • 2017-11-10
        • 1970-01-01
        • 2017-05-22
        • 2013-07-21
        相关资源
        最近更新 更多