TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

Members
Stats
  • Total Posts: 195,994
  • Total Topics: 21,325
  • Online today: 105
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online

[Block] Code to display image RSS feeds from Flickr, Picasa and Photobucket

Started by PeterLawrence, November 23, 2008, 10:19:12 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

PeterLawrence

Here is some code I've been using to display image feeds from Flickr, Picasa and Photobucket.
It works by scanning the RSS feed for the description fields and displaying the data (images and text) in a table.
I've included an example RSS feed for each photo gallery type in the $feedurls array, see top of code.
To obtain your own feed URL go to your gallery system, select your required photos and then click on the RSS symbol. This should give you an URL which then can be used in the $feedurls array in this PHP code.
Note this code only works with PHP version 5.
Example output provided in attached jpeg (see bottom of post).

// list your Picasa, Flickr and Photobucket RSS feeds here
$feedurls=array(
'http://api.flickr.com/services/feeds/photos_public.gne?id=31558111@N08&lang=en-us&format=rss_200', // flickr example
'http://feed123.photobucket.com/albums/o282/rszczepanski/Tryweryn280707/feed.rss', // photobucket example
'http://picasaweb.google.com/data/feed/base/user/junk.Jonandteresa?alt=rss&kind=album&hl=en_US&access=public', // Picasa example
);

foreach ($feedurls as $feedurl)
{
$photofeed = new PhotoRSSFeed($feedurl);
$photofeed -> outputTable(3); // the parameter specifies the number of columns to display
}
/*
Photo RSS Feed Class PHP 5
*/
class PhotoRSSFeed{
protected $photos=array();

public function PhotoRSSFeed($url)
    {
        $doc   = new DOMDocument();
        $doc->load($url);
        $xpath = new DOMXPath($doc);
$QueryRst=$xpath->query("/rss/channel/title/text()");
if ($QueryRst) {
$album = $QueryRst->item(0)->nodeValue;
}
else {
    $album='Unknown';
}

// scan for JPG image discription fields
        $photos_list = $xpath->query("/rss/channel/item");
        for($i=0;$i<$photos_list->length;$i++) {
    $QueryRst=$xpath->query("media:thumbnail/@url",$photos_list->item($i));
if ($QueryRst->length>0) {
$type = $QueryRst->item(0)->nodeValue;
}
if (empty($type)) {
$QueryRst=$xpath->query("media:content/media:thumbnail/@url",$photos_list->item($i));
if ($QueryRst->length>0) {
$type = $QueryRst->item(0)->nodeValue;
}
}
if (empty($type)) {
$type="jpg"; // if can't find a type just set it to jpg
}
            $type = substr($type,strlen($type)-3,strlen($type));
if (strtolower($type)=="jpg") {
$this->photos[$album][$i]["id"]    = "$i";
$QueryRst=$xpath->query("description/text()",$photos_list->item($i));
if ($QueryRst->length>0) {
$this->photos[$album][$i]["description"] = $QueryRst->item(0)->nodeValue;
}
}
        }
    }

// output the table of images
public function outputTable($Cols)
    {
$TableBackgroundCol = '#F2F5F7';
if (count($this->photos) == 0) {
echo '<div align="center"><br /><b>No Photos Found</b></br \></div>';
}
else {
foreach($this->photos as $albums_key => $albums_value)
{
    echo '<div align="center">';
echo $albums_key,'<br />';
echo '<table align="center" width="100%" cellspacing="1" style="clear: both; table-layout: fixed;">
<tr>';
$colcount=0;
// iterate through all photos of the album
foreach ($albums_value as $photos)
{
$colcount++;
if ($colcount>$Cols)
{
$colcount=1;
echo ' </tr>';
echo '  <tr>';
}
echo '<td valign="center" bgcolor="',$TableBackgroundCol,'" class="windowbg windowbg_hover" valign="middle" align="center">';
if (isset($photos["description"]))
echo $photos["description"],'<br />';
echo '</td>';
}

// add empty cells
for ($colcount=$colcount;$colcount<$Cols;$colcount++)
{
echo '
<td bgcolor="',$TableBackgroundCol,'" >
</td>';
}
echo '
</tr>
</table>';
}
echo '</div>';
}
}
}

P.S. You need to log into this forum to see the attached image.

futuro

hi, how can use this without tinyportal? the code give me the error

Fatal error: Class 'PhotoRSSFeed' not found in...

thanks

IchBin

You running php5? How are you using the code? Please give us some details.

futuro

php 5.2.6 and php 5.2.8

iÃ,´m try to put the code into a php-block for tiny portal and into a php single page...

the only change i make is remove the photobucket and picassa examples and change the rss flickr.

errors:
into php-block -> Fatal error: Call to undefined method domdocument::load() in...
into php-single page -> Fatal error: Class 'PhotoRSSFeed' not found in...

PeterLawrence

I've tested the above script under PHP 5.2.3 and it seems to work fine.
The script should run standalone.
Simply save the code in  a separate php file with <?php  inserted at the top and  ?> at the bottom of the file.

The error Fatal error: Call to undefined method domdocument::load() looks like an error that you would get if you were to try and run this script under PHP 4. However, if you were using PHP4 you would also get a warning about domdocument() expects at least 1 parameter.

Is it possible for you to post your modified script or the section of code that you've changed to this forum so I can take a look?

futuro

thanks... the web is not online yet, but this is the code:

<?php

function template_main()
{

global $context$settings$options$txt;

// list your Picasa, Flickr and Photobucket RSS feeds here
 
$feedurls=array(
'http://api.flickr.com/services/feeds/groups_pool.gne?id=986935@N20&lang=es-us&format=rss_200'
);

foreach (
$feedurls as $feedurl)
{
$photofeed = new PhotoRSSFeed($feedurl);
$photofeed -> outputTable(3); // the parameter specifies the number of columns to display
}
/*
Photo RSS Feed Class PHP 5
*/ 
class PhotoRSSFeed{
protected $photos=array();

public function PhotoRSSFeed($url)
    {
        
$doc   = new DOMDocument();
        
$doc->load($url);
        
$xpath = new DOMXPath($doc);
$QueryRst=$xpath->query("/rss/channel/title/text()");
if ($QueryRst) {
$album $QueryRst->item(0)->nodeValue;
}
else {
    $album='Unknown';
}

// scan for JPG image discription fields
        
$photos_list $xpath->query("/rss/channel/item");
        for(
$i=0;$i<$photos_list->length;$i++) {
    $QueryRst=$xpath->query("media:thumbnail/@url",$photos_list->item($i));
if ($QueryRst->length>0) {
$type $QueryRst->item(0)->nodeValue;
}
if (empty($type)) {
$QueryRst=$xpath->query("media:content/media:thumbnail/@url",$photos_list->item($i));
if ($QueryRst->length>0) {
$type $QueryRst->item(0)->nodeValue;
}
}
if (empty($type)) {
$type="jpg"// if can't find a type just set it to jpg
}
            
$type substr($type,strlen($type)-3,strlen($type));
if (strtolower($type)=="jpg") {
$this->photos[$album][$i]["id"]    = "$i";
$QueryRst=$xpath->query("description/text()",$photos_list->item($i));
if ($QueryRst->length>0) {
$this->photos[$album][$i]["description"] = $QueryRst->item(0)->nodeValue;
}
}
        }
    }

// output the table of images
public function outputTable($Cols)
    {
$TableBackgroundCol '#F2F5F7';
if (count($this->photos) == 0) {
echo '<div align="center"><br /><b>No Photos Found</b></br \></div>';
}
else {
foreach($this->photos as $albums_key => $albums_value)
{
    echo '<div align="center">'
echo $albums_key,'<br />';
echo '<table align="center" width="100%" cellspacing="1" style="clear: both; table-layout: fixed;">
<tr>'
;
$colcount=0;
// iterate through all photos of the album
foreach ($albums_value as $photos)
{
$colcount++;
if ($colcount>$Cols)
{
$colcount=1;
echo ' </tr>';
echo '  <tr>';
}
echo '<td valign="center" bgcolor="',$TableBackgroundCol,'" class="windowbg windowbg_hover" valign="middle" align="center">';
if (isset($photos["description"]))
echo $photos["description"],'<br />';
echo '</td>';
}

// add empty cells
for ($colcount=$colcount;$colcount<$Cols;$colcount++)
{
echo '
<td bgcolor="'
,$TableBackgroundCol,'" >
</td>'
;
}
echo '
</tr>
</table>'
;
}
echo '</div>'
}
}
}



}

?>


thanks

PeterLawrence

This is a simple one to fix. You need to move the class outside the function template_main.
try..

<?php
template_main
();

function 
template_main()
{

global $context$settings$options$txt;

// list your Picasa, Flickr and Photobucket RSS feeds here
    
$feedurls=array(
        
'http://api.flickr.com/services/feeds/groups_pool.gne?id=986935@N20&lang=es-us&format=rss_200'
        );
        
    foreach (
$feedurls as $feedurl)
    {
        
$photofeed = new PhotoRSSFeed($feedurl);
        
$photofeed -> outputTable(3); // the parameter specifies the number of columns to display
    
}
}
/*
Photo RSS Feed Class PHP 5
*/ 
class PhotoRSSFeed{
protected $photos=array();

public function PhotoRSSFeed($url)
    {
        
$doc   = new DOMDocument();
        
$doc->load($url);
        
$xpath = new DOMXPath($doc);
$QueryRst=$xpath->query("/rss/channel/title/text()");
if ($QueryRst) {
$album $QueryRst->item(0)->nodeValue;
}
else {
    $album='Unknown';
}

// scan for JPG image discription fields
        
$photos_list $xpath->query("/rss/channel/item");
        for(
$i=0;$i<$photos_list->length;$i++) {
    $QueryRst=$xpath->query("media:thumbnail/@url",$photos_list->item($i));
if ($QueryRst->length>0) {
$type $QueryRst->item(0)->nodeValue;
}
if (empty($type)) {
$QueryRst=$xpath->query("media:content/media:thumbnail/@url",$photos_list->item($i));
if ($QueryRst->length>0) {
$type $QueryRst->item(0)->nodeValue;
}
}
if (empty($type)) {
$type="jpg"// if can't find a type just set it to jpg
}
            
$type substr($type,strlen($type)-3,strlen($type));
if (strtolower($type)=="jpg") {
$this->photos[$album][$i]["id"]    = "$i";
$QueryRst=$xpath->query("description/text()",$photos_list->item($i));
if ($QueryRst->length>0) {
$this->photos[$album][$i]["description"] = $QueryRst->item(0)->nodeValue;
}
}
        }
    }

// output the table of images
public function outputTable($Cols)
    {
$TableBackgroundCol '#F2F5F7';
if (count($this->photos) == 0) {
echo '<div align="center"><br /><b>No Photos Found</b></br \></div>';
}
else {
foreach($this->photos as $albums_key => $albums_value)
{
    echo '<div align="center">'
echo $albums_key,'<br />';
echo '<table align="center" width="100%" cellspacing="1" style="clear: both; table-layout: fixed;">
<tr>'
;
$colcount=0;
// iterate through all photos of the album
foreach ($albums_value as $photos)
{
$colcount++;
if ($colcount>$Cols)
{
$colcount=1;
echo ' </tr>';
echo '  <tr>';
}
echo '<td valign="center" bgcolor="',$TableBackgroundCol,'" class="windowbg windowbg_hover" valign="middle" align="center">';
if (isset($photos["description"]))
echo $photos["description"],'<br />';
echo '</td>';
}

// add empty cells
for ($colcount=$colcount;$colcount<$Cols;$colcount++)
{
echo '
<td bgcolor="'
,$TableBackgroundCol,'" >
</td>'
;
}
echo '
</tr>
</table>'
;
}
echo '</div>'
}
}
}
?>



On another point I notice you're using Flickr. So you may be interested in one of my SMF mods
Flickr Gallery Browser

futuro

yes... im the same man... i canÃ,´t use your flickr gallry browser for a group :(

for this i wanto to try with this one...

futuro

now, the code give me a new error:

Fatal error: Call to undefined method domdocument::load() in ...\Sources\Load.php(1741) : eval()'d code on line 27

Sorry for this.

PeterLawrence

This problem could be related to how PHP5 is install on your system.
If you search the web for Call to undefined method domdocument::load php5 there are quite a number of articles.
Are you using PHP5 in safe mode?
I also read that with PHP 5 ona  windows system you may need to comment out the line php_domxml.dll in your php.ini file.


Also see http://uk.php.net/manual/en/dom.setup.php
and http://uk.php.net/manual/en/domdocument.load.php

futuro

thanks peter... no more error... but, now (sorry) donÃ,´t take any photo

No Photos Found

i use my own rss flickr and your example and nothing.

PeterLawrence

How did you fix the error? What happens if you try a different RSS feed?

futuro

the error was the php.ini

if I try a different RSS... nothing... always "no photos found"

i need some additional mod? :S

PeterLawrence

Not sure what the problem is here, since the test script I provided worked on my system.
Have you tried to directly specify the url in the PhotoRSSFeed statement?
For example take it out of the for loop and see what happens..

$photofeed = new PhotoRSSFeed('http://api.flickr.com/services/feeds/groups_pool.gne?id=986935@N20&lang=es-us&format=rss_200');
$photofeed -> outputTable(3);

Otherwise I can only suggest that you add a few output statements (echo) in the code to trace the problem.



futuro

try another... perhaps this errors...


Warning: DOMDocument::load() [domdocument.load]: URL file-access is disabled in the server configuration in *** on line 29

Warning: DOMDocument::load(***) [domdocument.load]: failed to open stream: no suitable wrapper could be found in *** on line 29

Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "***" in *** on line 29

PeterLawrence

Sorry jbernable but it looks like your service provider has disabled this functionality. Note the phpFlickr mod uses a different method to access RSS feeds which does not seem to be blocked by your server's set up.

I don't know if this is of any use to you, but I've posted some block code on the SMF forum site which uses phpFlick to display random images from a Flickr.
See http://www.simplemachines.org/community/index.php?topic=274558.msg1814677#msg1814677

It should be possible to modify that block code to get the images from a group rather than an account using the phpFlickr functions I mentioned in a previous post.

If I get the time in the next couple of weeks I may write the code for you, but I think you may have the skills to do it your self.


PeterLawrence

jbernable if you look at the phpFlickr site there is some example php code for displaying user groups which you may find useful
see http://phpflickr.com/examples/a-users-groups/source/

This website is proudly hosted on Crocweb Cloud Website Hosting.