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

362 views Asked by At

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?

2

There are 2 answers

0
Rishad On BEST ANSWER

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

0
JuliSmz On

Seems like $imageLink value is //?#, so if you do

if($imageLink = $enclosure->get_link())

The result is true...

check the exact value if there is no enclosure, and then change the condition... I.E

$imageLink = $enclosure->get_link();
if($imageLink !== "//?#") {

You can check the exact value using

if ($enclosure = $item->get_enclosure()){
    $imageLink = $enclosure->get_link(); 
    var_dump($imageLink);
}