我一直在寻找解决问题的方法,但似乎没有任何效果:(
我有以下代码
<?php
$rss = new DOMDocument();
$rss->load('http://thelostgirl2014.wordpress.com/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node)
{
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'content' => $node->getElementsByTagName('encoded')->item(0)->nodeValue,
'comments' => $node->getElementsByTagName('comments')->item(0)->nodeValue,
'photos' => $node->getElementsByTagNameNS('http://thelostgirl2014.wordpress.com/feed/', 'content')->item(0),
);
array_push($feed, $item);
}
$limit = 3;
for($x=0;$x<$limit;$x++)
{
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$content = $feed[$x]['content'];
$comments = $feed[$x]['comments'];
$photos = $feed[$x]['photos'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));?>
<div class="feed">
<div class="feed-title">
<a href="<?php echo $link?>" title="<?php echo $title?>"><?php echo $title?></a>
</div>
<div class="feed-date">
<small><em>Posted on <?php echo $date?></em></small>
</div>
<div class="feed-description">
<?php echo $photos?>
</div>
</div><?php
}?>
它会拉出主节点值但我无法获取命名空间值。
我试图获取媒体网址,以便我可以在我的php页面上显示图像但当前代码
'photos' => $node->getElementsByTagNameNS('http://thelostgirl2014.wordpress.com/feed/', 'content')->item(0),
什么都不返回:(
这是我试图检索数据的博客片段
<item>
<title>Achievements</title>
<link>http://thelostgirl2014.wordpress.com/2014/01/13/achievements/</link>
<comments>http://thelostgirl2014.wordpress.com/2014/01/13/achievements/#comments</comments>
<pubDate>Mon, 13 Jan 2014 03:20:32 +0000</pubDate>
<dc:creator><![CDATA[thelostgirl2014]]></dc:creator>
<category><![CDATA[Uncategorized]]></category>
<guid isPermaLink="false">https://thelostgirl2014.wordpress.com/?p=8</guid>
<description><![CDATA[Well, in the last few months I have achieved so much…from joining a cruiseship to simply eating prawns!! I have never been a prawn fan, never liked the look of them so never tried them! But today, at Mozarts Restaurant in Funchal, Madeira I had a fresh mushroom and prawn risotto which was delicious! To […]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thelostgirl2014.wordpress.com&blog=62611577&post=8&subd=thelostgirl2014&ref=&feed=1" width="1" height="1" />]]></description>
<content:encoded><![CDATA[<p><a href="http://thelostgirl2014.files.wordpress.com/2014/01/20140113-031825-am.jpg"><img src="http://thelostgirl2014.files.wordpress.com/2014/01/20140113-031825-am.jpg?w=696" alt="20140113-031825 am.jpg" class="alignnone size-full" /></a></p>
<p>Well, in the last few months I have achieved so much…from joining a cruiseship to simply eating prawns!! </p>
<p>I have never been a prawn fan, never liked the look of them so never tried them!<br />
But today, at Mozarts Restaurant in Funchal, Madeira I had a fresh mushroom and prawn risotto which was delicious! </p>
<p>To me this is a massive step for me, I have always been afraid of trying new food but seeing as I’m away I’m keen to try new things…so prawns are my latest thing…<br />
The best thing was…the three course meal came with free champagne and wine woohoo!! Bonus!! €37 well spent me thinks!!</p><br /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thelostgirl2014.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thelostgirl2014.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thelostgirl2014.wordpress.com&blog=62611577&post=8&subd=thelostgirl2014&ref=&feed=1" width="1" height="1" />]]></content:encoded>
<wfw:commentRss>http://thelostgirl2014.wordpress.com/2014/01/13/achievements/feed/</wfw:commentRss>
<slash:comments>0</slash:comments>
<georss:point>32.642638 -16.923540</georss:point>
<geo:lat>32.642638</geo:lat>
<geo:long>-16.923540</geo:long>
<media:content url="http://2.gravatar.com/avatar/5f747606b6bc78986250229230fe693f?s=96&d=identicon&r=G" medium="image">
<media:title type="html">thelostgirl2014</media:title>
</media:content>
<media:content url="http://thelostgirl2014.files.wordpress.com/2014/01/20140113-031825-am.jpg" medium="image">
<media:title type="html">20140113-031825 am.jpg</media:title>
</media:content>
</item>
任何返回媒体网址的帮助都会很棒, 路加
答案 0 :(得分:0)
您最好按照以下方式更改您的工作方式: $ curl = curl_init();
curl_setopt_array($curl, Array(
CURLOPT_URL => 'http://thelostgirl2014.wordpress.com/feed/',
CURLOPT_USERAGENT => 'spider',
CURLOPT_TIMEOUT => 120,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_ENCODING => 'UTF-8'
));
$data = curl_exec($curl);
curl_close($curl);
$feed = array();
$xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
foreach ( $xml->channel->item as $item ) {
$creator = $item->children('dc', TRUE);
$item = array (
'title' => $item->title,
'desc' => $item->description,
'author'=> $creator,
'link' => $item->link
);
array_push($feed, $item);
}
答案 1 :(得分:0)
SO的所有答案都说使用SimpleXMLELement。但如果我也不想要怎么办呢?
以下是答案:
$node->getElementsByTagNameNS('*', 'thumbnail')->item(0)