/*
lj.php - yet another LiveJournal image getter thingy
by Ryland Sanders
Friday, November 24, 2006
This source code is released into the public domain, copy it and modify it as you see fit. A link and credit would be nice, though.
This script gets the "latest images" XML stream from LiveJournal, parses it, filters it, and displays the resulting list as clickable links, limited to the number of images specified by the $limit parameter in the query string.
*/
echo "\r\n";
echo "\r\n";
echo "\t\r\n";
echo "\t
\r\n";
echo "\r\n";
echo "\r\n";
echo "\r\n";
echo "
LiveJournal images
\r\n";
echo '
LiveJournal images XML feed :: PHP source code for this page
' . "\r\n";
/* put script file name of this page in a variable (just in case you change the filename but forget to update the script with the new name) */
$thispage = getenv("SCRIPT_NAME");
/* get the 'limit' parameter from the query string. if the server is configured with register_globals turned on, it does this for you automatically, but I like to add zero to numerical values from the query string to make sure it's a number. if there's no 'limit' paramter in the query string, set it to the default, 100 images. */
$limit = $_GET['limit'] + 0;
if (!$limit) { $limit = 100; }
echo "
Number of images: " . $limit . " - Limit to: 25 :: 50 :: 100 :: 250\r\n";
echo "
\r\n";
/* this is a callback function used by array_filter() below to filter our any elements that aren't tagged as 'RECENT-IMAGE' in LJ's xml stream */
function is_img($recent_image) {
return $recent_image['tag'] == 'RECENT-IMAGE';
}
/* open the stream and assign it to a file handle variable */
$handle = fopen("http://www.livejournal.com/stats/latest-img.bml", "r");
/* dump the stream contents into a string variable */
$ljxml = "";
while (!feof($handle)) {
$ljxml .= fgets($handle);
}
/* close the stream */
fclose($handle);
/* create an XML parser object */
$p = xml_parser_create();
/* parse the stream data into an array $vals */
xml_parse_into_struct($p, $ljxml, $vals);
/* free the parser object, since we're done with it */
xml_parser_free($p);
/* filter the $vals array using the callback function above */
$imgs = array_filter($vals, "is_img");
/* get a subset of the array limited to the number of images in the $limit variable */
if ($limit < 250) { $imgs = array_slice($imgs, 0, $limit); }
/* loop through the array and print each array element as a clickable link */
$num_imgs = sizeof($imgs);
$ct = 1;
foreach ($imgs as $recent_image) {
echo "
LJ post URL ::\r\n";
echo "Image URL ::\r\n";
if ($ct > 1) {
echo "Previous image ::\r\n";
} else {
echo "Previous image ::\r\n";
}
if ($ct < $num_imgs) {
echo "Next image ::\r\n";
} else {
echo "Next image ::\n";
}
echo "Top of page\r\n";
echo "\r\n
\r\n\r\n";
$ct++;
}
echo "\r\n";
echo "\r\n";