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 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测