-1

I have a simple script that until yesterday had worked fine for 2 years. Im just taking a XML feed from a WP site and formatting it to be displayed on a different website. Here is the code:

<?php
function download_page($path){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$path);
    curl_setopt($ch, CURLOPT_FAILONERROR,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    $retValue = curl_exec($ch);          
    curl_close($ch);
    return $retValue;
}

$sXML = download_page('https://example.com/tradeblog/feed/atom/');
$oXML = new SimpleXMLElement($sXML);

$items = $oXML->entry;
$i = 0;
foreach($items as $item) {
    $title = $item->title;
    $link = $item->link;
    echo '<li>';
    foreach($link as $links) {
        $loc = $links['href'];
        $href = str_replace("/feed/atom/", "", $loc);
        echo "<a href=\"$href\" target=\"_blank\">";
    }
    echo $title;
    echo "</a>";;
    echo "</li>";
    if(++$i == 3) break;
}

?>

I can echo out $sXML and it will display the entire XML contents as expected. When I try and echo $oXML I get the 500 error. Any use of $oXML causes the 500. What changed? Is there a different / better way to do this using PHP?

Dirty Bird Design
  • 5,333
  • 13
  • 64
  • 121
  • 2
    [How to get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – miken32 Jan 25 '19 at 20:35
  • 1
    That Atom feed is returning invalid characters and, in consequence, your script fails too: https://validator.w3.org/feed/check.cgi?url=https%3A%2F%2Foptimusfutures.com%2Ftradeblog%2Ffeed%2Fatom%2F – cabrerahector Jan 25 '19 at 20:38
  • Since this is coming from a WP feed, is it best to try and skip or sanitize the tag that has the invalid character? – Dirty Bird Design Jan 25 '19 at 20:57
  • I try the other endpoints, /feed/, /feed/rss2/ etc and I get no more parsing errors in the returned feed. However the 500 error is again present when I use the $oXML = new SimpleXMLElement($sXML) variable. – Dirty Bird Design Jan 25 '19 at 21:51

2 Answers2

1

It seems your xml source is not exactly a xml. I tried to validate it using w3 scholl validator and it throws an error. Tried here too, and got the same error.

danielarend
  • 1,379
  • 13
  • 26
  • I switched to the rss2 feed, there are no errors in it - https://validator.w3.org/feed/check.cgi?url=https%3A%2F%2Foptimusfutures.com%2Ftradeblog%2Ffeed%2Frss2%2F However still outputs nothing on my page – Dirty Bird Design Jan 25 '19 at 21:19
1

Not sure why, but this worked

<?php
    $rss = new DOMDocument();
    $rss->load('https://example.com/tradeblog/feed/rss2/');

    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
    );
    array_push($feed, $item);
}

$limit = 3;
for($x=0;$x<$limit;$x++) {
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link = $feed[$x]['link'];

    echo '<li><a href="'.$link.'" title="'.$title.'">'.$title.'</a></li>';
}

?>
Dirty Bird Design
  • 5,333
  • 13
  • 64
  • 121