PHP Script to Display last 5 thumbnails...
sdmeyers
Registered Users Posts: 9 Beginner grinner
Hi,
I always thought that flickr thing was cool where you could embed thumbnails of your last five images on a webpage, so after playing around with the XML-RPC API for a bit I decided to write a PHP Script that emulates that.
I thought I'd post the first working version of this script for anyone who's interested to use, comment upon, etc.
Some notes...
Anyway, enjoy:
[php]
<!-- WARNING... Use this at you own risk... I accept no responsibility whatsoever for anything bad happening. -->
<!-- Please use this code however you want, however it would be nice to let me know if you make any interesting changes to it. -->
<!-- Thanks, Scott Meyers (scott@mydogateit.com) -->
<html>
<head><title>Last Five Images From Smugmug</title></head>
<body>
<?php
include("xmlrpc.inc"); // You need to get this from http://phpxmlrpc.sourceforge.net/ (BTW tnx to the developers!)
$nick = 'nick'; // replace with you nickname
$user_email = 'email'; // replace with your email/username (We aren't using this though)
$user_pass = 'password'; // replace with your password (I'd rather do this anonymously, but I get lot's of 'Invalid User' errors if I try)
$version = '1.1'; // Leave this ?
$xmlrpc_client = new xmlrpc_client("/xmlrpc/", "upload.smugmug.com", "443");
/* Debug? 1 = yes, 0 = no */
$xmlrpc_client->setDebug(0);
/*** get session id ***/
$login_struct = array(
new xmlrpcval($nick),
new xmlrpcval($user_pass),
new xmlrpcval($version));
$get_sesid_msg = new xmlrpcmsg('loginWithPassword', $login_struct);
// $get_sesid_msg = new xmlrpcmsg('loginAnonymously'); // This would be nice... but we start getting Invalid User errors beginning with getAlbums()
$ses_id = $xmlrpc_client->send($get_sesid_msg, '0', 'https');
if (!$ses_id) { die("send failed"); }
$ses_id_v = $ses_id->value();
if (!$ses_id->faultCode()) {
$id_array = xmlrpc_decode($ses_id_v);
$session_id = $id_array;
} else {
print "Fault: ";
print "Code: " . $ses_id->faultCode() . " Reason '" .$ses_id->faultString()."'<br>";
}
/*** Get Albums info ***/
$album_struct = array(
new xmlrpcval($session_id),
new xmlrpcval($nick));
$get_albums_msg = new xmlrpcmsg('getAlbums', $album_struct);
$albums = $xmlrpc_client->send($get_albums_msg, '0', 'https');
if (!$albums) { die("get album failed");}
$albums_v = $albums->value();
if (!$albums->faultCode()) {
$albums_array = xmlrpc_decode($albums_v);
} else {
print "Fault: ";
print "Code: " . $albums->faultCode() . " Reason '" .$albums->faultString()."'<br>";
}
/*** Now we need to get the Image ID's with getImages ***/
$imageIDs_collection = array();
foreach($albums_array as $album){
$get_imageIDs_struct = array(
new xmlrpcval($session_id),
new xmlrpcval($album, "int"));
$get_imageIDs_msg = new xmlrpcmsg('getImages', $get_imageIDs_struct);
$get_imageIDs = $xmlrpc_client->send($get_imageIDs_msg, '0', 'https');
if (!$get_imageIDs) { die("get images failed");}
$get_imageIDs_v = $get_imageIDs->value();
if (!$get_imageIDs->faultCode()) {
$imageIDs = xmlrpc_decode($get_imageIDs_v);
$imageIDs_collection = array_merge($imageIDs, $imageIDs_collection);
} else {
print "Fault: ";
print "Code: " . $get_imageIDs->faultCode() . " Reason '" .$get_imageIDs->faultString()."'<br>";
}
}
/*** Shall we sort the images by ID (I hope this works!) ***/
rsort($imageIDs_collection, SORT_NUMERIC);
/*** Now we only want the last 5 images ***/
/* This is making a hugh assumption about ImageIDs incrementally increasing without reuse to determine newest images...
I could use getImageInfo() for all images and use the "Date" string to do this properly, but that seems like a lot of
extra pinging of the server which would take a while for large image collections. */
array_splice($imageIDs_collection, 5);
/*** Now we attack the server again and get the URLs for our 5 remaining imageIDS ***/
foreach($imageIDs_collection as $imageID) {
settype($imageID, "integer");
$image_struct = array(
new xmlrpcval($session_id),
new xmlrpcval($imageID, "int"));
$get_image_info = new xmlrpcmsg('getImageInfo', $image_struct);
$image_info = $xmlrpc_client->send($get_image_info, '0', 'https');
if (!$image_info) { die("get image URL failed");}
$image_info_v = $image_info->value();
if (!$image_info->faultCode()) {
$image_info_array = xmlrpc_decode($image_info_v);
$image_albumID = $image_info_array;
} else {
print "Fault: ";
print "Code: " . $image_info->faultCode() . " Reason '" .$image_info->faultString()."'<br>";
}
print "<a href=\"http://mydogateit.smugmug.com/gallery/$image_albumID/1/$imageID\"><img border=\"1\" src=\"http://mydogateit.smugmug.com/photos/$imageID-Ti-1.jpg\"></a><br />";
/* Hacked together the above URL... I tried the following code but it seems getImageURLs() is broken. */
/* settype($imageID, "integer");
$image_url_struct = array(
new xmlrpcval($session_id),
new xmlrpcval($imageID, "int"));
$get_image_url = new xmlrpcmsg('getImageURLs', $image_url_struct);
$image_urls = $xmlrpc_client->send($get_image_url, '0', 'https');
if (!$image_urls) { die("get image URL failed");}
$image_urls_v = $image_urls->value();
if (!$image_urls->faultCode()) {
$image_url_array = xmlrpc_decode($image_url_v);
print $image_url_array.'<br />';
} else {
print "Fault: ";
print "Code: " . $image_urls->faultCode() . " Reason '" .$image_urls->faultString()."'<br>";
}
*/
}
?>
</body>
</html>
[/php]
- Scott
I always thought that flickr thing was cool where you could embed thumbnails of your last five images on a webpage, so after playing around with the XML-RPC API for a bit I decided to write a PHP Script that emulates that.
I thought I'd post the first working version of this script for anyone who's interested to use, comment upon, etc.
Some notes...
- This uses the XML-RPC for PHP stuff from http://phpxmlrpc.sourceforge.net/
- Because of all the back and forth XML-RPC stuff going on it takes some time to run this (speed is relative on lot's of things). I plan on taking this and caching the results on my website so it doesn't run whenever someone hit's my page (that's not good for anyone). Then this script can be run every 24 hours or so to keep the cache upto date. Ideally Sumgmug will someday create something like this (Hint, hint ;-).
- One idea would be to just create a link and run this general script when you add new images (Yes, that requires an extra step... of course someone could add a "run script when uploading files" type of option to any uploading applications.)
Anyway, enjoy:
[php]
<!-- WARNING... Use this at you own risk... I accept no responsibility whatsoever for anything bad happening. -->
<!-- Please use this code however you want, however it would be nice to let me know if you make any interesting changes to it. -->
<!-- Thanks, Scott Meyers (scott@mydogateit.com) -->
<html>
<head><title>Last Five Images From Smugmug</title></head>
<body>
<?php
include("xmlrpc.inc"); // You need to get this from http://phpxmlrpc.sourceforge.net/ (BTW tnx to the developers!)
$nick = 'nick'; // replace with you nickname
$user_email = 'email'; // replace with your email/username (We aren't using this though)
$user_pass = 'password'; // replace with your password (I'd rather do this anonymously, but I get lot's of 'Invalid User' errors if I try)
$version = '1.1'; // Leave this ?
$xmlrpc_client = new xmlrpc_client("/xmlrpc/", "upload.smugmug.com", "443");
/* Debug? 1 = yes, 0 = no */
$xmlrpc_client->setDebug(0);
/*** get session id ***/
$login_struct = array(
new xmlrpcval($nick),
new xmlrpcval($user_pass),
new xmlrpcval($version));
$get_sesid_msg = new xmlrpcmsg('loginWithPassword', $login_struct);
// $get_sesid_msg = new xmlrpcmsg('loginAnonymously'); // This would be nice... but we start getting Invalid User errors beginning with getAlbums()
$ses_id = $xmlrpc_client->send($get_sesid_msg, '0', 'https');
if (!$ses_id) { die("send failed"); }
$ses_id_v = $ses_id->value();
if (!$ses_id->faultCode()) {
$id_array = xmlrpc_decode($ses_id_v);
$session_id = $id_array;
} else {
print "Fault: ";
print "Code: " . $ses_id->faultCode() . " Reason '" .$ses_id->faultString()."'<br>";
}
/*** Get Albums info ***/
$album_struct = array(
new xmlrpcval($session_id),
new xmlrpcval($nick));
$get_albums_msg = new xmlrpcmsg('getAlbums', $album_struct);
$albums = $xmlrpc_client->send($get_albums_msg, '0', 'https');
if (!$albums) { die("get album failed");}
$albums_v = $albums->value();
if (!$albums->faultCode()) {
$albums_array = xmlrpc_decode($albums_v);
} else {
print "Fault: ";
print "Code: " . $albums->faultCode() . " Reason '" .$albums->faultString()."'<br>";
}
/*** Now we need to get the Image ID's with getImages ***/
$imageIDs_collection = array();
foreach($albums_array as $album){
$get_imageIDs_struct = array(
new xmlrpcval($session_id),
new xmlrpcval($album, "int"));
$get_imageIDs_msg = new xmlrpcmsg('getImages', $get_imageIDs_struct);
$get_imageIDs = $xmlrpc_client->send($get_imageIDs_msg, '0', 'https');
if (!$get_imageIDs) { die("get images failed");}
$get_imageIDs_v = $get_imageIDs->value();
if (!$get_imageIDs->faultCode()) {
$imageIDs = xmlrpc_decode($get_imageIDs_v);
$imageIDs_collection = array_merge($imageIDs, $imageIDs_collection);
} else {
print "Fault: ";
print "Code: " . $get_imageIDs->faultCode() . " Reason '" .$get_imageIDs->faultString()."'<br>";
}
}
/*** Shall we sort the images by ID (I hope this works!) ***/
rsort($imageIDs_collection, SORT_NUMERIC);
/*** Now we only want the last 5 images ***/
/* This is making a hugh assumption about ImageIDs incrementally increasing without reuse to determine newest images...
I could use getImageInfo() for all images and use the "Date" string to do this properly, but that seems like a lot of
extra pinging of the server which would take a while for large image collections. */
array_splice($imageIDs_collection, 5);
/*** Now we attack the server again and get the URLs for our 5 remaining imageIDS ***/
foreach($imageIDs_collection as $imageID) {
settype($imageID, "integer");
$image_struct = array(
new xmlrpcval($session_id),
new xmlrpcval($imageID, "int"));
$get_image_info = new xmlrpcmsg('getImageInfo', $image_struct);
$image_info = $xmlrpc_client->send($get_image_info, '0', 'https');
if (!$image_info) { die("get image URL failed");}
$image_info_v = $image_info->value();
if (!$image_info->faultCode()) {
$image_info_array = xmlrpc_decode($image_info_v);
$image_albumID = $image_info_array;
} else {
print "Fault: ";
print "Code: " . $image_info->faultCode() . " Reason '" .$image_info->faultString()."'<br>";
}
print "<a href=\"http://mydogateit.smugmug.com/gallery/$image_albumID/1/$imageID\"><img border=\"1\" src=\"http://mydogateit.smugmug.com/photos/$imageID-Ti-1.jpg\"></a><br />";
/* Hacked together the above URL... I tried the following code but it seems getImageURLs() is broken. */
/* settype($imageID, "integer");
$image_url_struct = array(
new xmlrpcval($session_id),
new xmlrpcval($imageID, "int"));
$get_image_url = new xmlrpcmsg('getImageURLs', $image_url_struct);
$image_urls = $xmlrpc_client->send($get_image_url, '0', 'https');
if (!$image_urls) { die("get image URL failed");}
$image_urls_v = $image_urls->value();
if (!$image_urls->faultCode()) {
$image_url_array = xmlrpc_decode($image_url_v);
print $image_url_array.'<br />';
} else {
print "Fault: ";
print "Code: " . $image_urls->faultCode() . " Reason '" .$image_urls->faultString()."'<br>";
}
*/
}
?>
</body>
</html>
[/php]
- Scott
0
Comments
Cheers!
I've already made a few tweaks to the code listing (i.e. I had my nick hardwired into the URLs I created <>). I decided to try to keep the latest version of this script available for download here. Any additions or changes should be noted at http://www.mydogateit.com/tech/. I'll also ping this forum for any major changes.
-Scott
All places i ask seem that the webmaster does not want to pass along the information.
I mentioned it was a slight mod to this type script in the way of
It will not call up previous / recent IMAGES
instead i am looking for a script that will call up
MOST RECENT POSTS
to my bulletine board
and display these results on a different page.
Does anyone know where i can find this which i can mod to fit my site?
If you want to email me off line I'd be glad to help.
-S.
but theres no possable way to
"email someone offline"
All emails originate and transfer online hahahaha
its ok
ill shoot you something in a min or two
thanks
Hello I am using this script and its giving me this error
Fault: Code: 16 Reason 'No CURL support compiled in.'
Fault: Code: 16 Reason 'No CURL support compiled in.'
Any Ideas?
Joaquin Encinas
As you can see from the code, I've tried a couple of things - using GET vs. POST, etc. Same results. It almost works! I get back the page, but not with the Slideshow style! It's like TemplateID=8 isn't taking. When I paste the URL from the CURLOPT_URL like below into a browser, it works as expected with the style as Slideshow. But when I run this PHP script below, it comes up defaulting to the SmugMug style.
I'm going to keep looking into this, but any ideas for troubleshooting this behavior?
THANK YOU!
Want faster uploading? Vote for FTP!