Racing Minis at Zandvoort
Photo: Copyright © 2019 Eelke Blok

WordPress Quick Flickr Widget with cURL

Of course, you put a site live, and you run into trouble. Turns out the widget I chose for displaying my Flickr photostream uses the file_get_contents() PHP function to fetch the contents of the Flickr feed. Whether this is at all possible is governed by the PHP setting allow_url_fopen, and my hosting provider has it turned off. This is not at all uncommon, because when a script is not coded carefully, it would be possible to load a remote file into a script that was only ever intended to load local files. Scary.

Anyway, luckily there is an alternative, which is called cURL. It too will allow you to open a remote URL, but in a more controlled fashion. Also, when using it, there is no doubt at all whether the programmer was intending to use a remote file.

The following patch will add this to the current version of Quick Flickr Widget, which is 1.2.10.
Index: C:/Data/Project/blokspeed/code/wp-content/plugins/quick-flickr-widget/quick_flickr_widget.php
===================================================================
--- C:/Data/Project/blokspeed/code/wp-content/plugins/quick-flickr-widget/quick_flickr_widget.php (revision 189)
+++ C:/Data/Project/blokspeed/code/wp-content/plugins/quick-flickr-widget/quick_flickr_widget.php (revision 206)
@@ -95,7 +95,7 @@
// Output via php or javascript?
if (!$javascript)
{
- eval("?>". file_get_contents($url) . "<?");
+ eval("?>". quickflickr_geturl($url) . "<?");
$photos = $feed;

if ($random) shuffle($photos["items"]);
@@ -180,7 +180,7 @@
if (!ereg("http://api.flickr.com/services/feeds", $newoptions["username"])) // Not a feed
{
global $flickr_api_key;
- $str = @file_get_contents("http://api.flickr.com/services/rest/?method=flickr.people.findByUsername&api_key=".$flickr_api_key."&username=".urlencode($newoptions["username"])."&format=rest");
+ $str = @quickflickr_geturl("http://api.flickr.com/services/rest/?method=flickr.people.findByUsername&api_key=".$flickr_api_key."&username=".urlencode($newoptions["username"])."&format=rest");
ereg("

if ($findByUsername["stat"] == "ok")
@@ -302,4 +302,22 @@
}
}
add_action("init", "quickflickr_widgets_init");
+
+function quickflickr_geturl($url) {
+ $response = '';
+
+ if(function_exists('curl_version')) {
+ // If curl_version exists, we'll assume CURL is available and we'll use it
+ // in preference to url_fopen
+ $ch = curl_init($url);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+ $response = curl_exec($ch);
+ curl_close($ch);
+ }
+ else {
+ $response = file_get_contents($url);
+ }
+
+ return $response;
+}
?>
As an added bonus, here's something else you might find useful if you use this widget. I didn't like the alt and title attributes the widget put on my photo thumbnails, so I changed it to use the title of the photo as entered on Flickr.
Index: C:/Data/Project/blokspeed/code/wp-content/plugins/quick-flickr-widget/quick_flickr_widget.php
===================================================================
--- C:/Data/Project/blokspeed/code/wp-content/plugins/quick-flickr-widget/quick_flickr_widget.php (revision 188)
+++ C:/Data/Project/blokspeed/code/wp-content/plugins/quick-flickr-widget/quick_flickr_widget.php (revision 189)
@@ -120,7 +120,7 @@
$href = ($thickbox) ? $photo_medium_url : $photo_link;

$photo_title = ($show_titles) ? "

$photo_title

" : "";
- $out .= $before_item . "\"$photo_description\"$photo_title" . $after_item;
+ $out .= $before_item . "\""$photo_title" . $after_item;
}
$flickr_home = $photos["link"];
}
Update 8/6/2010 Konstantin, the author of Quick Flickr Widget, has responded on his own blog that actually, the appropriate way to deal with different server environments and the availability of url_fopen and cURL would be to use WordPress' built-in WP_Http object. It currently works for me, so I doubt I'll go and change the code, but maybe if you run into this problem, find this post and have some programming skills, you could create a patch to do that.

Comments

great! i don't know how to update the flickr widget on my site. how to update the Quick Flickr Widget? where should i place this patch?

In reply to by george (not verified)

If you are a programmer, your development environment (e.g. Eclipse) would typically have a way to apply patch files (what file to apply it to is contained in the patch). You could also read the patch file and apply the changes manually, it is a standard format. If you are not a programmer, I wouldn't recommend trying to apply the patch, but instead request the author of the plugin to include the patch. In fact, he has responded on his own blog <a href="http://kovshenin.com/wordpress/plugins/quick-flickr-widget/?allcomments…; rel="nofollow">that WordPress contains a better way to make sure that for outside connectivity, the appropriate mechanism is used</a>.

Add new comment

Category