SimpleXML loop works but breaks halfway

I have a loop going through the youtube feed and it works fine, but by the end it fails with an 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 returns from youtube perfectly, and there are certain results, than where it breaks down, any ideas why it will do it?

Edit: Tested in place using wamp and it works great. Not yet on the server. Live, thumbnails after element 24 no thumbnails.

+2
php youtube-api simplexml
source share
5 answers

The error message shows that it can be split into this line:

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

Is it possible that you get the result without thumbnails? You do not seem to check that the collection of thumbnails contains at least one.

+2
source share

When you grab a node using children (), it may not return a valid value (which is why you get a warning), then you are trying to perform an operation on a bad object that causes an error, PHP will allow you to wrap an error handler to get more detailed information.

I would probably just check the return value of children ().

0
source share

Make sure that PHP 5 is installed correctly on the server. Since the SimpleXML extension requires PHP 5. If it is installed, include it from the php.ini file.

0
source share

Are you sure you are not serializing / deserializing your nodes anywhere? Even implicitly, for example. using sessions or ORM? "Node no longer exists" is a typical mistake in situations where the node, parent node, or owner document is no longer alive. (We had this when storing nodes in a session without first inserting them into rows.)

Is the code you published 100% the same code as the one you use on your development server in real time? How do you run it (command line or CGI or Apache module)? Are the versions the same?

I cannot reproduce the error, but given enough information, I could.

0
source share

I use the same snippet and everything seems to work just fine, I even made minor changes, and it works fine :)

0
source share

All Articles