(my current php page code at end of page) (using latest MODx Revolution)
I have a problem with converting a working PHP page into snippets, or generally MODx. As far as I know, snippets can handle PHP easily, and so far I converted all the echoes to returns and tested it out pretty much the same, and it didn’t work (it was called correctly as [[mysnippet]] but nothing shows up in the content area). I’ve tested out the page itself and it works. I actually have no clue how to convert this page to snippets/MODx, even after reviewing the docs on MODx.
My working php page in action (really basic right now):
http://www.hammerhead226.org/flickrtest/example.php
My php code: (with echoes intact)
<?php
//flickr variables
$flickrApiKey = 'b0e9db9193f9d4bddb2d42a81c7b351f';
$flickrUserId = '57059624@N06';
function flickrCall($params) {
$encoded_params = array();
foreach ($params as $k => $v){
$encoded_params[] = urlencode($k).'='.urlencode($v);
}
#
# call the API and decode the response
#
$url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);
$rsp = file_get_contents($url);
$rsp_obj = unserialize($rsp);
if($rsp_obj['stat']=='ok') {
return($rsp_obj);
}
else{
return(false);
}
}
// *** detailed photo view ***
if($photo = $_GET['photo']) {
//poll flickr data
$params = array(
'api_key' => $flickrApiKey,
'user_id' => $flickUserId,
'format' => 'php_serial',
'method' => 'flickr.photos.getInfo',
'photo_id' => $photo
);
if($photo_info = flickrCall($params)) {
$params = array(
'api_key' => $flickrApiKey,
'user_id' => $flickUserId,
'format' => 'php_serial',
'method' => 'flickr.photos.getSizes',
'photo_id' => $photo
);
$photo_sizes = flickrCall($params);
$photo_info[photo][sizes] = $photo_sizes[sizes][size];
unset($photo_sizes);
//print_r($photo_info);
}
else {
print "Flickr API Error!";
}
// display code
return('<h1>'.$photo_info[photo][title][_content].'</h1>
');
return('<img src="'.$photo_info[photo][sizes][4][source].'" width="'.$photo_info[photo][sizes][4][width].'" height="'.$photo_info[photo][sizes][4][height].'" />
');
return('<div id="description">.'.$photo_info[description][_content].'.</div>');
// end display
}
// *** photo list (for photoset ***
elseif($pset = $_GET['pset']){
return('<ul class="photoflow">
');
//poll flickr data
$params = array(
'api_key' => $flickrApiKey,
'user_id' => $flickUserId,
'format' => 'php_serial',
'method' => 'flickr.photosets.getPhotos',
'photoset_id' => $pset
);
if($photos = flickrCall($params)) {
//print_r($photos);
foreach($photos[photoset][photo] as &$photo) {
$params = array(
'api_key' => $flickrApiKey,
'user_id' => $flickrUserId,
'format' => 'php_serial',
'method' => 'flickr.photos.getSizes',
'photo_id' => $photo[id],
);
$photo_sizes = flickrCall($params);
$photo[thumbSrc] = $photo_sizes[sizes][size][1][source];
$photo[thumbW] = $photo_sizes[sizes][size][1][width];
$photo[thumbH] = $photo_sizes[sizes][size][1][height];
//display each photo
return(' <li><a href="?photo='.$photo[id].'"><img src="'.$photo[thumbSrc].'" width="'.$photo[thumbW].'" height="'.$photo[thumbH].'" /></a></li>
');
}
unset($photo);
//print_r($photos);
}
else {
print "Flickr API Error!";
}
// end display
return('</ul>
');
}
// *** photoset list ***
else {
return('<ul class="photoflow">
');
//poll flickr data
$params = array(
'api_key' => $flickrApiKey,
'user_id' => $flickrUserId,
'format' => 'php_serial',
'method' => 'flickr.photosets.getList',
);
if($photosets = flickrCall($params)) {
//print_r($photosets);
foreach ($photosets['photosets']['photoset'] as &$photoset) {
$params = array(
'api_key' => $flickrApiKey,
'user_id' => $flickUserId,
'format' => 'php_serial',
'method' => 'flickr.photosets.getPhotos',
'photoset_id' => $photoset[id]
);
if($photos = flickrCall($params)) {
//print_r($photos);
$photoset['thumbPhotoId'] = $photos['photoset']['photo'][0]['id'];
$params = array(
'api_key' => $flickrApiKey,
'user_id' => $flickrUserId,
'format' => 'php_serial',
'method' => 'flickr.photos.getSizes',
'photo_id' => $photoset['thumbPhotoId'],
);
$photo_sizes = flickrCall($params);
$photoset['thumbSrc'] = $photo_sizes['sizes']['size'][1]['source'];
$photoset['thumbW'] = $photo_sizes['sizes']['size'][1]['width'];
$photoset['thumbH'] = $photo_sizes['sizes']['size'][1]['height'];
//print_r($photo_sizes);
//display each photoset
return(' <li><a href="?pset='.$photoset['id'].'"><img src="'.$photoset['thumbSrc'].'" width="100px" height="100px" /><span>'.$photoset['title']['_content'].'</span></a></li>
');
}
else {
print "Flickr API Error!";
}
}
unset($photoset);
}
else {
print "Flickr API Error!";
}
// end display
return('</ul>
');
}
?>