Search code examples
phpsimplepie

PHP SimplePie Error: $item->get_enclosure() always return true


Am trying to build a news reader using php SimplePie Library. When i try to get image from feed using code

if ($enclosure = $item->get_enclosure()){
    $imageLink = $enclosure->get_link(); 
    echo "<img src=\"$imageLink\">";
}

When i fetch feed from an rss feed which dont have an enclosure, it echo image tag with source as follows.

src="//?#"

The above code is working fine with feeds which have enclosures.

I also tried with code:

if ($enclosure = $item->get_enclosure()){
    if($imageLink = $enclosure->get_link()){
        echo "<img src=\"$imageLink\">";
    }
}

can someone tell me what i am doin wrong in these codes?


Solution

  • Check if $imageLink is assigned a value anywhere in your code. Most probably that could be the error. Use print_r or var_dump at each step of your code to fine where exactly is you code assigning that value to before mentioned variable