问题
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.
回答1:
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.
回答2:
When you're grabbing the node using children() it might not be passing back a valid value (which is why you get the warning) then you're trying an operation on the bad object which is causing the error. PHP will allow you to wrap the error handler to get more detailed information.
I would probably just check the return value from children().
回答3:
Make sure that PHP 5 is properly installed in the server. Because the SimpleXML
extension requires PHP 5. If installed then enable it from php.ini file.
回答4:
Are you sure you are not serializing/deserializing your nodes anywhere? Not even implicitly, e.g. using sessions or ORMs? "Node no longer exists"
is a typical error for situations where the node, a parent node or the owner document are no longer "alive". (We had it when storing nodes in the session, without casting them to strings first.)
Is the code you posted 100% the exact same code as the one you are running on both your development and live server? How are you running it (command line or CGI or Apache module)? Are the versions the same?
I cannot reproduce the error, but given enough information, I might.
回答5:
I'm using the same snippet and everything seems to be working great I've even made little changes and it works great : )
来源:https://stackoverflow.com/questions/3248972/simplexml-loop-works-but-breaks-half-way-through