SimpleXML 循环有效但中途中断 [英] SimpleXML loop works but breaks half way through

查看:23
本文介绍了SimpleXML 循环有效但中途中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环遍历来自 youtube 提要的结果,它工作正常,但最终失败并出现错误:

警告:main() [function.main]:第 8 行的 ../youtubereader.php 中不再存在节点警告:main() [function.main]:第 8 行的 .../youtubereader.php 中不再存在节点致命错误:在第 9 行.../youtubereader.php 中的非对象上调用成员函数 attributes()

我的代码是:

entry as $entry) {$media = $entry->children('media', true);$watch = (string)$media->group->player->attributes()->url;$thumbnail = (string)$media->group->thumbnail[0]->attributes()->url;?><div class="videoitem"><div class="videothumb"><a href="<?php echo $watch; ?>"class="watchvideo"><img src="<?php echo $thumbnail;?>"alt="<?php echo $media->group->title; ?>"/></a></div><div class="videotitle"><h3><a href="<?php echo $watch; ?>"class="watchvideo"><?php echo $media->group->title;?></a></h3><p><?php echo $media->group->description;?></p>

<?php $i++;if($i==3) { echo '<div class="clear small_v_margin"></div>';$i=0;} } ?

我的 xml 很好地从 youtube 返回,结果肯定比它坏的地方多,知道为什么会这样做吗?

在本地测试,使用 wamp,它工作正常.还是不在服务器上.Live,第24项之后的缩略图不返回缩略图.

解决方案

从错误信息看可能是在这一行断了:

$thumbnail = (string)$media->group->thumbnail[0]->attributes()->url;

您是否可能得到没有缩略图的结果?您似乎没有检查缩略图集合是否至少包含一个.

I have a loop going through results from a youtube feed and it works fine but towards the end it fails with the error:

Warning: main() [function.main]: Node no longer exists in ../youtubereader.php on line 8
Warning: main() [function.main]: Node no longer exists in .../youtubereader.php on line 8
Fatal error: Call to a member function attributes() on a non-object in .../youtubereader.php on line 9

My code is:

<?php
error_reporting(E_ALL);
$feedURL = 'http://gdata.youtube.com/feeds/api/users/USERNAME/uploads?max-results=50';
$sxml = simplexml_load_file($feedURL);
$i=0;
foreach ($sxml->entry as $entry) {
      $media = $entry->children('media', true);
      $watch = (string)$media->group->player->attributes()->url;
      $thumbnail = (string)$media->group->thumbnail[0]->attributes()->url;
      ?>
      <div class="videoitem">
        <div class="videothumb"><a href="<?php echo $watch; ?>" class="watchvideo"><img src="<?php echo $thumbnail;?>" alt="<?php echo $media->group->title; ?>" /></a></div>
        <div class="videotitle">
            <h3><a href="<?php echo $watch; ?>" class="watchvideo"><?php echo $media->group->title; ?></a></h3>
            <p><?php echo $media->group->description; ?></p>
        </div>
      </div>      
<?php $i++; if($i==3) { echo '<div class="clear small_v_margin"></div>'; $i=0; } } ?>

My xml comes back from youtube fine and there are definitely more results than where it breaks, any ideas why it would do this?

Edit: Tested locally, using wamp and it works fine. Still not on server. Live, the thumbnails after item 24 no thumbnails are returned.

解决方案

It appears from the error message that it may be breaking on this line:

$thumbnail = (string)$media->group->thumbnail[0]->attributes()->url;

Is it possible you're getting a result with no thumbnails? You don't seem to be checking that the thumbnail collection contains at least one.

这篇关于SimpleXML 循环有效但中途中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆