simple_xml – Call to a member function on a non object – PHP

  php

Q(Question):

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!!

A(Answer):

Hi.

Have you tried turning on the debug messages?

Try that and let us know what that does.

A(Answer):

Heya, pezholio.

SimpleXML is tricky. Try this:


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

A(Answer):

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

A(Answer):

Heya, pezholio.

SimpleXML is tricky. Try this:


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

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

A(Answer):

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! 🙂

A(Answer):

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.

LEAVE A COMMENT