Search code examples
phpapiflickrmodxphpflickr

phpFlickr - multiple albums on one page not working - single album works


I'm trying to implement the phpFlickr-wrapper (https://github.com/dan-coulter/phpflickr) in my MODX-project.

I tried a simple code i found here on stackoverflow and put it in a snippet called "Flickr":

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

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

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

$mySetID = $album; 

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

The snippet call:

[[!Flickr? &setname=`[[+tv.FickrID]]`]]

The TV "FlickrID" holds the ID of the wanted album.

This is working fine for one album. But when I try to output a second gallery the output gets stopped after the first snippet-call. The second time the snippet-call is made it stops with this entry in the MODX error-log:

 ...75.include.cache.php : 18) PHP warning: Invalid argument supplied for foreach()

How can I display more than one album on the same page, any idea?


Solution

  • Problem solved...

    I just had to include the content from the "config.php"-file inside the snippet, rather than referring to it by the "require_once"-line of code.

    <?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;