PHP Script to Display last 5 thumbnails...

sdmeyerssdmeyers 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...
  • 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

Comments

  • NikolaiNikolai Registered Users Posts: 19,035 Major grins
    edited February 8, 2005
    Thanks for sharing!
    Cheers!1drink.gif
    "May the f/stop be with you!"
  • sdmeyerssdmeyers Registered Users Posts: 9 Beginner grinner
    edited February 9, 2005
    Revisions to code
    I've already made a few tweaks to the code listing (i.e. I had my nick hardwired into the URLs I created <11doh.gif>). 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
  • Liqid TouchLiqid Touch Registered Users Posts: 2 Beginner grinner
    edited June 18, 2005
    Ive been looking for a modification of a script like this for some time now.
    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?
  • sdmeyerssdmeyers Registered Users Posts: 9 Beginner grinner
    edited June 18, 2005
    Ive been looking for a modification of a script like this for some time now.
    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.
  • Liqid TouchLiqid Touch Registered Users Posts: 2 Beginner grinner
    edited June 19, 2005
    Sorry SD
    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

    thanksrolleyes1.gif
  • jencinasjencinas Registered Users Posts: 4 Beginner grinner
    edited October 11, 2006
    USing this script
    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
  • sdmeyerssdmeyers Registered Users Posts: 9 Beginner grinner
    edited October 11, 2006
    Yep... PHP need to have CURL support. CURL is program that handles file-transfers that is common in the Unix/Linux world. If you are using PHP on Windows that may the problem (not sure if CURL is available there). Either way whoever compiled PHP didn't --include-curl.
    jencinas wrote:
    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
  • jencinasjencinas Registered Users Posts: 4 Beginner grinner
    edited October 12, 2006
    Thank you I will talk to our support people and see if they can instal CURL in our server
  • NimaiNimai Registered Users Posts: 564 Major grins
    edited September 20, 2007
    I was searching for someone who was using CURL to do tricks with SmugMug because I'm puzzled about the results I'm getting!
    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?
    ini_set('display_errors','1');
    ini_set('display_startup_errors','1');
    error_reporting(E_ALL & ~E_NOTICE);
    
    $ch = curl_init();    // initialize curl handle
    curl_setopt($ch, CURLOPT_URL, "http://nimai.smugmug.com/homepage/templatechange.mg?origin=http://nimai.smugmug.com%2Fkeyword%2FWHS-cheerleading-2007&TemplateID=8" );
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); // times out after 11s
    //curl_setopt($ch, CURLOPT_POST, 1); // set POST method
    //curl_setopt($ch, CURLOPT_POSTFIELDS, "origin=http://nimai.smugmug.com%2Fkeyword%2FWHS-cheerleading-2007&TemplateID=8"); // add POST fields
    $result = curl_exec($ch); // run the whole process
    curl_close($ch); 
    
    echo $result;
    //echo htmlspecialchars( $result,  ENT_QUOTES);
    

    THANK YOU!
  • SamirDSamirD Registered Users Posts: 3,474 Major grins
    edited July 22, 2009
    Does anyone know if the php in the first post still works? I'm thinking of adapting it for a project that I'm working on.
    Pictures and Videos of the Huntsville Car Scene: www.huntsvillecarscene.com
    Want faster uploading? Vote for FTP!
Sign In or Register to comment.