donglvhe7591 2015-03-04 19:12
浏览 46
已采纳

如何从PHP中的tumlbr rss源获取第一张图像

0Here is the relevant part of my rss feed:

    <channel>
        <description></description>
        <title>Untitled</title>
        <generator>Tumblr (3.0; @xxx)</generator>
        <link>http://xxx.tumblr.com/</link> 
        <item>
            <title>Title</title>
            <description>&lt;figure&gt;&lt;img src="https://31.media.tumblr.com/c78c7t3abd23423549d3bb0f705/tumblr_inline_nkp9z234d0uj.jpg"/&gt;&lt;/figure&gt;</description>
            <link>http://xxx.tumblr.com/post/99569244093</link>
            <guid>http://xxx.tumblr.com/post/99569244093</guid>
            <pubDate>Thu, 09 Oct 2014 11:19:33 -0400</pubDate>
        </item>
    </channel>

Using the answer from other questions on here I tried this:

$content = file_get_contents("http://xxx.tumblr.com/rss"); 
$feed = new SimpleXmlElement($content); 
$imgs = $feed->channel->item[0]->description->xpath('//img');
    foreach($imgs as $image) {
            echo (string)$image['src'];     
};

This is returning an empty array for $imgs

Does it have something to do with the tags being &lt; &gt; etc?

and if so what can I do?

  • 写回答

3条回答 默认 最新

  • duanfu3884 2015-03-04 20:28
    关注

    You can get it from the description, which seems to include a HTML image tag for the image, by using a simple regular expression with preg_match:

    $content = file_get_contents("http://xxx.tumblr.com/rss");
    $feed    = new SimpleXmlElement($content);
    $img     = (string)$feed->channel->item[0]->description;
    
    if (preg_match('/src="(.*?)"/', $img, $matches)) {
        $src = $matches[1];
        echo "src = $src", PHP_EOL;
    }
    

    Output:

    src = http://40.media.tumblr.com/58d24c3009638514325b113859ba369f/tumblr_nk0mwfhKXU1sl87kjo1_500.jpg
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀