duan1930
2018-06-06 12:44
浏览 52
已采纳

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?

图片转代码服务由CSDN问答提供 功能建议

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

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

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

  src =“//?#”
   
 
 

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

我也试过代码:

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

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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongxie2749 2018-11-13 20:05
    已采纳

    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

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douweinu8562 2018-06-06 12:53

    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);
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题