PHP SimplePie错误:$ item-> get_enclosure()始终返回true

PHP SimplePie错误:$ item-> get_enclosure()始终返回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?

我正在尝试使用php SimplePie Library构建新闻阅读器。 当我尝试使用代码 p>

  if($ enclosure = $ item-&gt; get_enclosure()){
 $ imageLink = $ enclosure-&gt; get_link  ();  
 echo“&lt; img src = \”$ imageLink \“&gt;”; 
} 
  code>  pre> 
 
 

当我从没有的RSS提要中获取Feed时 一个外壳,它回显带有源的图像标签如下。 p>

  src =“//?#”
  code>  pre> 
 
 

以上代码适用于带有附件的源。 p>

我也试过代码: p>

  if($ enclosure = $  item-&gt; get_enclosure()){
 if($ imageLink = $ enclosure-&gt; get_link()){
 echo“&lt; img src = \”$ imageLink \“&gt;”; 
} 
  } 
  code>  pre> 
 
 

有人能告诉我这些代码中我做错了什么吗? p> div>

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

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);
}