simple_xml - Call to a member function on a non object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pezholio
    New Member
    • Jun 2007
    • 22

    simple_xml - Call to a member function on a non object

    Hi,

    I'm having a few problems using the simple_xml and the Flickr API. I'm trying to get a random image from a group pool and print out the results. It works fine when I enter a photo id manually, but when I try and generate a random number I get the above error. Here's my code:

    [PHP]
    function findAttribute($ object, $attribute) {
    foreach($object->attributes() as $a => $b) {
    if ($a == $attribute) {
    $return = $b;
    }
    }
    if($return) {
    return $return;
    }
    }

    $file = "http://flickr.com/services/rest/?method=flickr. groups.pools.ge tPhotos&api_key =API-KEY&group_id=67 4456@N21&per_pa ge=500";
    $xml = simplexml_load_ file($file);
    $total = $xml->photos['total'];
    $photoid = rand(1, $total);
    $photo = $xml->photos->photo[$photoid]; /* This is what's causing me problems */
    $server = findAttribute($ photo, server);
    $id = findAttribute($ photo, id);
    $secret = findAttribute($ photo, secret);
    $title = findAttribute($ photo, title);
    $user = findAttribute($ photo, ownername);
    $image = "http://static.flickr.c om/". $server ."/". $id . "_". $secret . ".jpg";
    ?>
    <img src="<?php echo $image; ?>" />
    <p><?php echo $title; ?> by <?php echo $user; ?></p>
    [/PHP]

    I think I know what I'm doing wrong, I just don't know how to rectify it - help!!
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    Have you tried turning on the debug messages?

    Try that and let us know what that does.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, pezholio.

      SimpleXML is tricky. Try this:

      [code=php]
      $photo = $xml->photos[0]->photo[$photoid]

      // Or, depending on how the XML is structured:
      $photo = $xml->photos[$photoid];
      [/code]

      Comment

      • pezholio
        New Member
        • Jun 2007
        • 22

        #4
        Originally posted by Atli
        Hi.

        Have you tried turning on the debug messages?

        Try that and let us know what that does.
        OK, here's what I get:

        Warning: rand() expects parameter 2 to be long, object given in /home/default/lichfielddc.gov .uk/user/htdocs/flickr.php on line 30

        Fatal error: Call to a member function attributes() on a non-object in /home/default/lichfielddc.gov .uk/user/htdocs/flickr.php on line 17

        Any ideas?

        Cheers

        Comment

        • pezholio
          New Member
          • Jun 2007
          • 22

          #5
          Originally posted by pbmods
          Heya, pezholio.

          SimpleXML is tricky. Try this:

          [code=php]
          $photo = $xml->photos[0]->photo[$photoid]

          // Or, depending on how the XML is structured:
          $photo = $xml->photos[$photoid];
          [/code]
          Hi pbmods,

          Neither of those work I'm afraid, if I replace $photoid with an arbitary number, say '4', in my original code it works fine, it's just when I replace it with the random string that I get the problem.

          Cheers

          Comment

          • pezholio
            New Member
            • Jun 2007
            • 22

            #6
            Now that's weird, I seem to have got it working now! No idea what I've done, but there seemed to be a problem somewhere around the rand() function. Thanks for your help guys! :)

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Yes, according to the messages you posted, the second parameter you passed into the rand function was an object, where PHP was expecting a number.

              I would guess that your "$xml->photos['total']" element either had an attribute you needed to fetch or an extra node that had to be included before getting the value.

              But, in any case, glad you got it to work :)

              P.S.
              The mt_rand function works exactly like the rand function, only 4 times faster. You should consider using that instead.

              Comment

              Working...