duanhongqiong9460 2018-10-19 19:07
浏览 67

从rss feed解析图像

This is the code i have right now. I'm trying to grab the image of this site but the variable returns nothing meaning i can't grab it ive been looking for hours

$html = "";

 $url = "https://www.idownloadblog.com/tag/jailbreak/rss";
 $xml = simplexml_load_file($url);
 for($i = 0; $i < 1; $i++){

$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$description = $xml->channel->item[$i]->description;
$pubDate = $xml->channel->item[$i]->pubDate;
$image= $xml->channel->item[$i]->content;


$box1 .= "<a><b>$title</b></a>"; 
$dbox1 .= "$description"; 



}
  • 写回答

1条回答 默认 最新

  • dougou6213 2018-10-23 15:13
    关注

    Looking at the RSS feed you provided, it does not appear there is a site image. So if you want the site's logo, you may have to statically link that.

    If you are wanting to get the post's image, we can do that. Here's what I would do.

    I created a package to make xml parsing a breeze. You can find it here: https://github.com/mtownsend5512/xml-to-array

    Then do the following:

    $xml = \Mtownsend\XmlToArray\XmlToArray::convert(file_get_contents('https://www.idownloadblog.com/tag/jailbreak/feed/'));
    

    Now you have a nice php array of the rss feed.

    Next, we will create a helper function to get the first image out of the post's body. We'll use this as the post's featured image.

    function getPostImage($content)
    {
        $output = preg_match_all('/<img[^>]+src=[\'"]([^\'"]+)[\'"][^>]*>/i', $content, $matches);
        if (empty($matches[1][0])) {
            return 'http://yoursite.com/images/fallback-image.jpg';
        }
        return $matches[1][0];
    }
    

    You'll want to replace http://yoursite.com/images/fallback-image.jpg with the url to your fallback image if there is no image in the post.

    Now, we loop through the posts:

    foreach ($xml['channel']['item'] as $post) {
        $title = $post['title']);
        $link = $post['link'];
        $description = $post['description'];
        $pubDate = $post['pubDate'];
        $image = getPostImage($post["content:encoded"]);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数