curl patch for flickrstickr

here's a quick patch to flickrstickr.module that causes it to use curl if available, and fail back to fopen as a last resort. I needed this to get this module working on Dreamhost.com because they don't allow fopen($url)

--- flickrstickr.module.orig 2006-07-18 23:17:23.000000000 -0700
+++ flickrstickr.module 2006-07-18 23:33:44.000000000 -0700
@@ -29,11 +29,9 @@
function flickrstickr_proxy_user() {
    header("Content-type: text/xml; charset=UTF-8");
    $apikey= flickrstickr_getapikey();
-   $fp= fopen("http://www.flickr.com/services/rest/?method=flickr.people.findByUsername&api_key=".$apikey."&username=".urlencode(arg(3)), "r");
-   while (!feof($fp)) {
-      $buffer= fgets($fp, 4096);
-      echo $buffer;
-   }
+   $query = "http://www.flickr.com/services/rest/?method=flickr.people.findByUsername&api_key=".$apikey."&username=".urlencode(arg(3));
+   $buffer = get_page($query);  
+   echo $buffer;
}
/**
  * public ajax proxy because the javascript  restriction
@@ -53,11 +51,8 @@
       if (arg(4) != null)
          $query .= "&tags=".urlencode(arg(4));
    }
-   $fp= fopen($query, "r");
-   while (!feof($fp)) {
-      $buffer= fgets($fp, 4096);
-      echo $buffer;
-   }
+   $buffer = get_page($query);
+   echo $buffer;
}
/**
  * implement hook_nodeapi to 4.6
@@ -338,16 +333,9 @@
   }
   //$url = flickrstickr_getflickrurl($matches[1],$matches[2],$matches[3],'o',$orient);
   $url = $sizes[$max]['url'];
-    $sfp = fopen($url,"rb");
+           $res = get_page($url);
   $dfp = fopen($path, 'wb');
-    if (!$sfp) die("Unable to open $url\n");
-
-    while (!feof($sfp)){
-    $res = fgets($sfp);
-    fwrite($dfp, $res);
-    }
-
-    fclose($sfp);
+    fwrite($dfp, $res);
   fclose($dfp);
   
   $default_nid = variable_get('flickrstickr_nodetemplate', '');
@@ -409,12 +397,7 @@
    $apikey = flickrstickr_getapikey();
    $userid = urlencode(arg(3));
    $query= "http://www.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=".$apikey."&photo_id=".$photo_id."&secret=".$secret;
-   $fp= fopen($query, "r");
-   $xml = "";
-   while (!feof($fp)) {
-      $buffer= fgets($fp, 4096);
-      $xml.= $buffer;
-   }
+   $xml = get_page($query);
    //die($xml);  
    $dom = new DOMDocument();
    $dom->loadXML($xml);
@@ -430,12 +413,7 @@
    $apikey = flickrstickr_getapikey();
    $userid = urlencode(arg(3));
    $query= "http://www.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=".$apikey."&photo_id=".$photo_id;
-   $fp= fopen($query, "r");
-   $xml = "";
-   while (!feof($fp)) {
-      $buffer= fgets($fp, 4096);
-      $xml.= $buffer;
-   }
+   $xml = get_page($query);
    //die($xml);  
    $dom = new DOMDocument();
    $dom->loadXML($xml);
@@ -467,7 +445,25 @@
   return $form;
}

-
-
+function get_page($url) {
+   if(function_exists('curl_init')) {
+      // hopefully curl is installed
+      $ch = curl_init($url);
+      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+      $raw = curl_exec($ch);
+      if (curl_errno($ch)) {
+         print curl_error($ch);
+         exit;
+      }
+      curl_close($ch);
+   } else { // slow-as-hell fallback
+      $fh = fopen($url, 'r');
+      while(!feof($fh)) {
+         $raw .= fread($fh, 4096);
+      }
+      fclose($fh);
+   }
+   return $raw;
+}

?>

new flickrstickr is ready!

Ian Ward - September 10, 2006 - 12:01

The new version of Flickrstickr is ready! It does not support curl alternative yet but it should not be hard to get it in there. Let me know if you're still interested in getting this in the code. Read about it then try it!.

Some improvements include -
- Click and drag into tinyMCE enabled textareas
- scroll through all your images in the dock, not just your 10 latest on a specific tag
- image selector is now floatable/dockable for getting to textareas off your visible screen
- integration with lightbox2 module
- storage of images as nodes and in the filesystem for local backup if flickr changes something

cheers,
Ian

 
 

Drupal is a registered trademark of Dries Buytaert.