-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflickr.php
103 lines (68 loc) · 2.81 KB
/
flickr.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
// Get settings
require_once('config.php');
// Cache file
$cache_file = 'cache/flickr.txt';
// Time that the cache was last filled.
$cache_file_created = @file_exists( $cache_file ) ? @filemtime( $cache_file ) : 0;
$image_found = false;
// Show file from cache if still valid.
if ( time() - $flickr_cachetime < $cache_file_created ) {
$image_found = true;
// Display images from the cache.
@readfile( $cache_file );
} else {
// Fetch the RSS feed from Flickr
$url = "http://api.flickr.com/services/feeds/photos_public.gne?id=$your_flickr_id&format=rss";
// Initiate the curl session
$ch = curl_init();
// Set the URL
curl_setopt( $ch, CURLOPT_URL, $url );
// Removes the headers from the output
curl_setopt( $ch, CURLOPT_HEADER, 0 );
// Return the output instead of displaying it directly
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
// Execute the curl session
$rss_feed = curl_exec( $ch );
// Close the curl session
curl_close( $ch );
// Parse the RSS feed to an XML object.
$rss_feed = @simplexml_load_string( $rss_feed );
if( isset( $rss_feed ) ) {
$image = $rss_feed->channel->item;
// Error check: Make sure there is at least one item
if( count( $image ) ) {
// Start output buffering.
ob_start();
// Open the flickr wrapping element.
$images = '';
$image_found = true;
for( $i = 0; $i < $flickr_limit; $i++ ) {
// Get thumbnail size
preg_match( '/<img[^>]*>/i', $image[$i]->description, $image_tag );
preg_match( '/(?<=src=[\'|"])[^\'|"]*?(?=[\'|"])/i', $image_tag[0], $image_src );
if ( preg_match( '/(_m.jpg)$/',$image_src[0] ) ){
$thumb = preg_replace('/(_m.jpg)$/', '_s.jpg', $image_src[0] );
} elseif( preg_match( '/(_m.png)$/',$image_src[0] ) ){
$thumb = preg_replace('/(_m.png)$/', '_s.png', $image_src[0] );
} elseif( preg_match( '/(_m.gif)$/',$image_src[0] ) ){
$thumb = preg_replace( '/(_m.gif)$/', '_s.gif', $image_src[0] );
}
$image_link = $image[$i]->link;
$image_title = $image[$i]->title;
$images .= "<li><a href='$image_link' title='$image_title' target='_blank'><img src='$thumb' alt='$image_title'></a></li>\n";
}
// Close the flickr wrapping element.
echo $images;
// Generate a new cache file.
$file = @fopen( $cache_file, 'w' );
// Save the contents of output buffer to the file, and flush the buffer.
@fwrite( $file, ob_get_contents() );
@fclose( $file );
ob_end_flush();
}
}
}
// In case the RSS feed did not parse or load correctly, show a link to the Flickr account
if ( !$image_found )
echo $images = "<li>Oops, our Flickr feed is unavailable at the moment - <a href='http://flickr.com/$your_flickr_id/' target='_blank'>Check our images on Flickr!</a></li>";