user5214732
user5214732

Reputation:

returning json array with PHP not working

I am trying to return a json array after i parse an rss feed.

this is my code :

<?php


header('Content-Type: application/json');
$feed = new DOMDocument();
//http://www.espnfc.com/rss
//http://www.football365.com/topical-top-10/rss
$feed->load('http://www.espnfc.com/rss');
$json = array();

$items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item');

$json['item'] = array();
$i = 0;
foreach($items as $item) {
   $i = $i+1;

   $title = $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue; 
   $link = $item->getElementsByTagName('link')->item(0)->firstChild->nodeValue;
   $img = $item->getElementsByTagName('enclosure')->item(0)->attributes->getNamedItem('url')->value;
   //$img = $item;echo($url);

   $json['item'][] = array("title"=>str_replace(array("\n", "\r", "\t","'"), ' ', $title),"link"=>str_replace(array("\n", "\r", "\t","'"), ' ', $link),"img"=>str_replace(array("\n", "\r", "\t","'"), ' ', $img));

}
print_r($json['item'][0]);
//echo json_encode($json['item']);

?>

after iterating all items i finally would like to echo them as a result:

echo json_encode($json['item']);

the problem that's not showing any thing in browser. but when i moved this line into the foreach bloc it show result (of course with redundancy).

Upvotes: 0

Views: 80

Answers (2)

GramThanos
GramThanos

Reputation: 3622

Your code returns request status "Status Code:500 Internal Server Error"

You can easy see it by browsing the network tab of your browser's web tools.

This is because on the 3rd post there is no image.

<?php
    // Json Header
    header('Content-Type: application/json');

    // Get Feed
    $feed = new DOMDocument();
    $feed->load('http://www.espnfc.com/rss');

    // Get Items
    $items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item');

    // My json object
    $json = array();
    $json['item'] = array();

    // For each item
    foreach($items as $item){
        // Get title
        $title = $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
        // Get link
        $link = $item->getElementsByTagName('link')->item(0)->firstChild->nodeValue;
        // Get image if it exist
        $img = $item->getElementsByTagName('enclosure');
        if($img->length>0){
            $img = $img->item(0)->attributes->getNamedItem('url')->value;
        } else {
            $img = "";
        }

        array_push($json['item'], array(
            "title" => preg_replace('/(\n|\r|\t|\')/', ' ', $title),
            "link" => preg_replace('/(\n|\r|\t|\')/', ' ', $link),
            "img" => preg_replace('/(\n|\r|\t|\')/', ' ', $img)
        ));
    }
    echo json_encode($json['item']);
?>

Upvotes: 1

Barmar
Barmar

Reputation: 780929

Some of the items don't have an <enclosure> tag, so the script gets an error when it tries to access the url attribute. You need to check for this.

$enclosures = $item->getElementsByTagName('enclosure');
if ($enclosures->length) {
    $img = $item->getElementsByTagName('enclosure')->item(0)->attributes->getNamedItem('url')->value;
} else {
    $img = '';
}

Upvotes: 2

Related Questions