By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,889 Members | 1,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,889 IT Pros & Developers. It's quick & easy.

simple_xml - Call to a member function on a non object

pezholio
P: 22
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.getPhotos&api_key=API-KEY&group_id=674456@N21&per_page=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.com/". $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!!
Oct 8 '08 #1
Share this Question
Share on Google+
6 Replies


Atli
Expert 5K+
P: 5,058
Hi.

Have you tried turning on the debug messages?

Try that and let us know what that does.
Oct 8 '08 #2

pbmods
Expert 5K+
P: 5,821
Heya, pezholio.

SimpleXML is tricky. Try this:

Expand|Select|Wrap|Line Numbers
  1. $photo = $xml->photos[0]->photo[$photoid]
  2.  
  3. // Or, depending on how the XML is structured:
  4. $photo = $xml->photos[$photoid];
  5.  
Oct 9 '08 #3

pezholio
P: 22
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
Oct 9 '08 #4

pezholio
P: 22
Heya, pezholio.

SimpleXML is tricky. Try this:

Expand|Select|Wrap|Line Numbers
  1. $photo = $xml->photos[0]->photo[$photoid]
  2.  
  3. // Or, depending on how the XML is structured:
  4. $photo = $xml->photos[$photoid];
  5.  
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
Oct 9 '08 #5

pezholio
P: 22
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! :)
Oct 9 '08 #6

Atli
Expert 5K+
P: 5,058
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.
Oct 9 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.