views:

89

answers:

4

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.

+2  A: 

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.

Eggplant Jeff
Yep thumbnails are returned by the feed for every item (YouTube does it for you).
Ashley
Rechecked this today, it seems the thumbnail is the problem. Locally, they all all returned but live YouTube isn't returning them. Any ideas why?
Ashley
Not really, I haven't used YouTube's API myself. I have however seen people occasionally complain about YouTube taking a while to make thumbnails, so it is probably worthwhile for your code to handle the "no thumbnail" case anyway (display a generic image or whatever). This does seem like something odd though. Are there any parameters you can pass in the querystring that affect thumbnails?
Eggplant Jeff
Don't think there are any parameters that can be passed over, either way the code is exactly the same live and local and it works locally. These videos are from 2007 so not producing the thumbnails shouldn't be a problem. Again, the code's the same, it must be something either to do with the server (media temple) or the way youtube handles requests from local servers rather than live
Ashley
A: 

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().

infamouse
Children works ok, it's the thumnail that isn't being returned. They are locally, but not live
Ashley
A: 

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.

chanchal1987
A: 

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.

janmoesen
Exact same code. On the live server if i skip over getting the thumnail it returns all the other values ok
Ashley
@Ashley: and what are the environments (server, OS, PHP version, …) on both?
janmoesen