0

嗨,我使用此代码调用 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;
4

1 回答 1

2

出于安全原因,服务器通常会关闭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-10-01T07:44:47.357 回答