Domanda

i am using DOMDocument() to include RSS feed in my code. However i get this error:

URL file-access is disabled in the server configuration

and thats because my server doesnt allow me either to modify the php.ini file or to set allow_url_fopen to ON.

Is there a workaround for this? This is my full code:

<?php
$rss = new DOMDocument();
$rss->load('rss.php');

$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 = 5;
echo '<table>';
for($x=0;$x<$limit;$x++) {
 $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
 $link = $feed[$x]['link'];

 echo <<<EOF
 <tr>
  <td><a href="$link"><b>$title</b></a></td>
 </tr>
EOF;
}
echo '</table>';
?>

Thank you.

È stato utile?

Soluzione

Okay, i solved it myself.

<?php

$k = 'rss.php';
$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $k);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $rss = curl_exec($ch);
    curl_close($ch);

    $xml = simplexml_load_string($rss, 'SimpleXMLElement', LIBXML_NOCDATA);

    $feed = array();
    foreach($xml->channel->item as $item){
     $item = array (
     'title' => $item->title,
     'desc' => $item->description,
     'link' => $item->link,
     'date' => $item->pubDate,
     );
     array_push($feed, $item);
    }
$limit = 5;
echo '<table>';
for($x=0;$x<$limit;$x++) {
 $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
 $link = $feed[$x]['link'];
 echo <<<EOF
 <tr>
  <td><a href="$link"><b>$title</b></a></td>
 </tr>
EOF;
}
echo '</table>';
?>

Altri suggerimenti

Use cURL commands. You really should be using this for server to server interactions rather than trying to pass URL's to constructors anyways.

Here is cURL documentation - http://us1.php.net/curl

I also have a simple cURL based REST client you can feel free to use - https://github.com/mikecbrant/php-rest-client

Basically, all you are looking to do is use cURL to retrieve the remote content instead of trying to open it directly using fopen wrapper. Once you retrieve the content then you pass it in to DOMDocument.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top