[SOLVED] Flickr snippet with phpFlickr - working first time, but not for 2nd, 3rd, etc

Hi there!

Another problem I’ve ran into is the following:

I created a snippet to grab the photos off a flickr-album with the phpFlickr-wrapper. For doing so I created the following Flickr-snippet:

<?php

$album = $modx->getOption('setname',$scriptProperties,''); // get the flickr-album ID from the snippet-call

//include the core file
if(!class_exists("phpFlickr")) require_once './assets/phpflickr-master/phpFlickr.php';
if(!class_exists("PEAR")) require_once './assets/phpflickr/PEAR/PEAR.php'; 

// include the config file
require_once('./assets/phpflickr-master/config.php');

$f = new phpFlickr($key, $api_secret);

$f->enableCache("fs", "./assets/phpflickr-master/cache");

$mySetID = $album; 
$mySet = $f->photosets_getPhotos($mySetID, NULL, NULL);

foreach ($mySet['photoset']['photo'] as $photo) {
    $output .= '<div><a rel="lightbox[]" href="'. $f->buildPhotoURL($photo, 'large') .'"><img src="'. $f->buildPhotoURL($photo, 'medium') .'" alt="" /></a></div>';
}

return $output;

I’m using three different resources with a pagetitle and (right now for testing purposes I am using) the longtitle-input for the flickr-album-id:

[[Flickr? &setname=`[[+longtitle]]`]]

This results in something like that for example:

[[Flickr? &setname=`72157711365499999`]]

Then in my template I’m having a getResources call like this:

[[getResources? &parents=[[*id]] &limit=0 &sortdir=asc &sortby=menuindex &tpl=galeries]]

The galeries-template looks like this:

<div class="gallery">
    <a href="[[~[[+id]]]]">[[+pagetitle]]</a>
    [[Flickr? &setname=`[[+longtitle]]`]]
</div>

When I run the snippet by loading the page which holds the getResources-call, the first album gets outputted.

But the second and third one do not appear at all. The error-log has the following entry:

[2019-12-13 16:59:21] (ERROR @ …/modx/core/cache/includes/elements/modsnippet/75.include.cache.php : 19) PHP warning: Invalid argument supplied for foreach()

So this error is related to the foreach-part. But why does this work the first time the snippet gets called, and not on any further calls?

I also tried to not use the getResources-call and to use 2 or 3 separate snippet-calls instead, but with the same issue (only getting the first album displayed). Additionally I tried to use the getResources-call and the Flickr-call cached and uncached, but no difference on that.

Any help on this is highly appreciated as well…and thank you for your time!

1 Like

Pls try adding an exclamation point (!) at the beginning of the call. That makes the call uncached, you are possibly getting the first one in and are unable to get the second one in

`[[!getResources? &parents=` [[*id]] ` &limit=` 0 ` &sortdir=` asc ` &sortby=` menuindex ` &tpl=` galeries `]]`

Thanks for answering here as well nuan88! I appreciate your will to help a lot!

So I already tried your suggestion, as well without success.

1 Like

Ah sorry, I hadn’t noticed that.

Isn’t this below specifying one album only?

$mySetID = $album; 
$mySet = $f->photosets_getPhotos($mySetID, NULL, NULL);

yes, but it is called several times in separate snippet-calls…you think it must habe an individual variable-name?

like:

$mySetID-1 = $album;
$mySet-1 = $f->photosets_getPhotos($mySetID-1, NULL, NULL);

for the first snippet-call…and:

$mySetID-2 = $album;
$mySet-2 = $f->photosets_getPhotos($mySetID-2, NULL, NULL);

for the second one, and so on?

1 Like

You have multiple calls, are you getting multiple outputs of first album? If all those calls are the same, you should get the same output multiple times.

Actually no, I think it should be like this

$mySetID = $album1; 
$mySet = $f->photosets_getPhotos($mySetID, NULL, NULL);

Then you would get the output of the album called album1, and your next call could be for album2 and so on.

The snippet is looking for a value of mySetID, that value I don’t believe can be changed…but this is getting way out of my area. You can make each call with a different album that way, all with the same mySetID value, I think it will just be reset each time.

For that error, my suggestion is to load the page three times successively, and see if you get three identical errors. That way we can match the error up clearly with this call. Its possibly not related but its hard to tell.

That’s poor advice and I very strongly disagree. Always cache, unless there’s a reason you can’t. While it’s great that you’re so active on the forums, @nuan88, please make sure what you tell people is right otherwise you’re only sending people on a wild goose chase.

@profilneurotiker When you say “the second and third one do not appear at all”, are just the second and third albums missing, or are you not seeing the link/pagetitle either?

I think I got it going now!

And thanks markh for posting here as well!

I agree with mark to cache as often as it is possible, so I do my getresources-call and the snippet-call inside the chunk uncached now!

What I did now was simple insert the content of the “config.php”-file inside the snippet itself. The “config.php”-file only holds the $api_key, $api_secret and $username from Flickr.

This is my getResources-call:

[[getResources? &parents=`[[*id]]` &sortdir=`asc` &sortby=`menuindex` &tpl=`galeries`]]

The getResources-template-chunk “galeries” looks like this:

<div class="gallery">
    [[Flickr? &setname=`[[+tv.flickr-album-ID]]`]]
</div>

And the snippet does have this content:

<?php
$album = $modx->getOption('setname',$scriptProperties,'');

//include the core file
if(!class_exists("phpFlickr")) require_once './assets/phpflickr-master/phpFlickr.php';
if(!class_exists("PEAR")) require_once './assets/phpflickr/PEAR/PEAR.php'; 

$key = "<flickr-api-key>";
$api_secret = "<flickr-api-secret>";
$username="<flickr-username>";

$f = new phpFlickr($key, $api_secret);

$f->enableCache("fs", "./assets/phpflickr-master/cache");

$mySetID = $album; 
$mySet = $f->photosets_getPhotos($mySetID, NULL, NULL);
foreach ($mySet['photoset']['photo'] as $photo) {
    $output .= '<div><a rel="lightbox[]" href="'. $f->buildPhotoURL($photo, 'large') .'"><img src="'. $f->buildPhotoURL($photo, 'medium') .'" alt="" /></a></div>';
}

return $output;

Now I do get all three albums in a slick-slider and with lightbox.

With some further tweaking I guess I can retrieve the album-description and other information off the related flickr-album.

Thanks for the correction, I deleted the random comment. I monitor my comments closely based on my knowledge base, don’t worry AFAIK no one has been led on a goosechase.

I get some things wrong, appreciate the advice.

@profilneurotiker That’s great news!