0

I'm currently struggling to parse an xml-feed with php. Unfortunately, I'm not lucky with that.

Here's my code:

<?php
echo 'starting parser';
$rss = simplexml_load_file('http://www.faz.net/rss/aktuell/wirtschaft/');
?>
//tests
<h1><?php echo $rss->title; ?></h1>
<h1><?php echo $rss->channel->title; ?></h1>
<h1><?php echo $rss->rss->channel->title; ?></h1>

<ul>
<?php

foreach($rss->channel->item as $chan) {  
        echo "<li><a href=\"".$chan->link."\">";
        echo $chan->title;     
        echo "</a></li>\n";
}  
?>

No output at all... Can anyone help me with that?

Akaino
  • 1,025
  • 6
  • 23
  • I've already seen that thread. However I tried a load of tutorials. It seems that the foreach is never entered. – Akaino Sep 19 '14 at 08:59
  • @Akaino : I just ran your script in my local and it worked. – Mithun Satheesh Sep 19 '14 at 09:02
  • Hmm... so Could there be a problem with my PHP installation on my server? – Akaino Sep 19 '14 at 09:02
  • @Akaino you need to check php.ini that there is xml parse or not. and also hit the URL in browser to check it accessiable to you. as the code is working for me. – Code Lღver Sep 19 '14 at 09:04
  • first turn on `display_errors` by editing your php.ini and check if there are any errors triggered. I am getting an error for `$rss->rss->channel->title`. But surely it works with the for loop and prints list items. – Mithun Satheesh Sep 19 '14 at 09:04
  • It seems that there is no php.ini. It's a website via domain.com and I just put the test.php file to the webspace and opened it via URL. Could you check sloth-soft.com/PHPReader/reader.php please? – Akaino Sep 19 '14 at 09:27

1 Answers1

0

Your approach is correct, I have tried it on my local ubuntu server and it works (at least what is left from your code):

<?php
echo 'starting parser';
$rss = simplexml_load_file('http://www.faz.net/rss/aktuell/wirtschaft/');
?>
<h1><?php echo $rss->channel->title; ?></h1>
<ul>
    <?php
    foreach ($rss->channel->item as $chan) {
        echo "<li><a href=\"" . $chan->link . "\">";
        echo $chan->title;
        echo "</a></li>\n";
    }
    ?>
</ul>

The reason that you don't see any output can be in getting the data from http://www.faz.net/rss/aktuell/wirtschaft/ so I suggest to test it:

<?php
echo 'starting parser';
$rss = simplexml_load_file('http://www.faz.net/rss/aktuell/wirtschaft/');
echo "<pre>".htmlspecialchars(var_export($rss, true))."</pre>";
?>

Also I would suggest to add try catch.

Zeusarm
  • 1,038
  • 6
  • 14
  • Thank you so far! I'm still struggling to get any output. I can't even find a php.ini. The file is stored on a domain.com webserver and accessible via sloth-soft.com/PHPReader/reader.php – Akaino Sep 19 '14 at 09:31
  • Use `phpinfo()` function to see where is `php.ini`. If you are on shared hostiong you can use `.htaccess` to set some changes in php configuration – Zeusarm Sep 19 '14 at 09:35
  • I found the .htaccess. But adding flags couses the php to crash :/ 'added php_flag display_startup_errors on' for example – Akaino Sep 19 '14 at 09:44