您好我使用此代码调用RSS xml文件中的最后一个条目,它在本地和其他服务器中运行良好,所以我猜是allow_url_fopen=on;
,但即使我设置了这个,我也得不到答案,我不回应任何rss feed,问题是代码没问题,在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(' & ', ' & ', $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;
答案 0 :(得分: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(' & ', ' & ', $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>';
}
?>