Search code examples
phpini

Alternative for DOMDocument()


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.


Solution

  • 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>';
    ?>