【问题标题】:Load External file PHP , php.ini allow_url_fopen=on;加载外部文件 PHP , php.ini allow_url_fopen=on;
【发布时间】:2013-10-01 07:28:16
【问题描述】:

嗨,我使用此代码调用 RSS xml 文件中的最后一个条目,它在本地和其他服务器中运行良好,所以我猜是 allow_url_fopen=on;,但即使我设置了这个,我也没有得到答案,我不要回显任何 rss 提要,问题是代码没问题,在 localhost 中工作正常

我该如何解决这个问题,我要启用什么参数或如何在 php.ini 中使用

代码

<?php


    $rss = new DOMDocument();
    $rss->load('http://www.laesquinadelamoda.com/feed/');
    $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 = 1;
    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 '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
        //echo '<small><em>Posted on '.$date.'</em></small></p>';
        //echo '<p>'.$description.'</p>';
        echo '"'.$title.'" - <span class="ital"><i><a href="'.$link.'" title="'.$title.'">Ver</a></i></span>';
    }
?>

php.ini

; As of 4.0b4, PHP always outputs a character encoding by default in
; the Content-type: header.  To disable sending of the charset, simply
; set it to be empty.
;
; PHP's built-in default is text/html
default_mimetype = "text/html"
;default_charset = "utf-8"

allow_url_fopen=on;

【问题讨论】:

  • 开启 url fopen 就像开启 C99 shell。真的,使用 curl+simplexml 或 curl+domdocument。

标签: php rss


【解决方案1】:

出于安全原因,服务器通常会关闭allow_url_fopen。使用curl 尝试以下代码。它应该可以工作。

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 0);

function curl_load( $url ) {
        $ch = curl_init();

        // set URL and other appropriate options
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // grab URL and store the content in $return variable
        $return = curl_exec($ch);

        // close cURL resource, and free up system resources
        curl_close($ch);

        return $return;
}

    $rss = new DOMDocument();
    $rss->loadXML( curl_load('http://www.laesquinadelamoda.com/feed/') );
    $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 = 1;
    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 '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
        //echo '<small><em>Posted on '.$date.'</em></small></p>';
        //echo '<p>'.$description.'</p>';
        echo '"'.$title.'" - <span class="ital"><i><a href="'.$link.'" title="'.$title.'">Ver</a></i></span>';
    }
?>

【讨论】:

    猜你喜欢
    • 2013-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    • 2011-03-28
    相关资源
    最近更新 更多