我刚接触RSS feed解析并遇到一些麻烦。我如何使用simplepie捕获以下资源?
<enc:enclosure resource="http://images.craigslist.org/00202_lQ0CCpDIPk0_300x300.jpg" type="image/jpeg"/>
答案 0 :(得分:0)
您可以使用get_enclosures()功能。
$feed = new SimplePie();
$feed->set_feed_url('http://simplepie.org/blog/feed/');
$feed->init();
$feed->handle_content_type();
foreach ($feed->get_items() as $item)
{
foreach ($item->get_enclosures() as $enclosure)
{
echo $enclosure->embed();
}
}
答案 1 :(得分:0)
以前的响应者没有意识到SimplePie是如何工作的。您不使用get_enclosures(),因为XML未格式化为<enclosure>
...
相反,你这样做:
require( 'autoloader.php' );
$feed = new SimplePie();
$feed->set_feed_url('https://denver.craigslist.org/search/msa?format=rss&query=left%20handed%20%7C%20lefthanded%20%7C%20lefty');
$feed->init();
$feed->handle_content_type();
foreach ($feed->get_items() as $item) {
$encs = $item->get_item_tags( 'http://purl.oclc.org/net/rss_2.0/enc#', 'enclosure' );
if ( !isset( $encs ) )
continue;
foreach ( $encs as $enclosure){
if ( !isset( $enclosure['attribs'] ) )
continue;
foreach ( $enclosure['attribs'] as $attr ) {
echo "\n" . $attr['resource'];
}
}
}