How to store a file downloaded by drupal_http_request ? And use drupal file API for this or not?

igorik - July 4, 2009 - 12:13

Hi,

I have a very small module, and with this I want to fetch image (weather) from external site using drupal_http_request, then I want to store it, and then to load the stored one in this module block.

The reason why to store it is then I can cache module block (and refresh it e.g. each 6 hours) and don't download external weather image with each page load and for every user as it is now when I have <img src="external url to img" /> in the block.

I can use drupal_http_request

$http_result = drupal_http_request($url);

if ($http_result->code == 200) {
        return _my_weather_block_content($http_result->data);
}

And NOW I WOULD LIKE TO STORE $data as file into files.
But I don't know if to use drupal file functions (and which) or if in this case it makes no sense to use drupal file api (because I don't need db settings, I need only stored image for loading into the block) and to use only php file functions...?

Many thanks for your hint
Igor
http://www.somvprahe.sk

Much simpler

yelvington - July 4, 2009 - 12:27

Much simpler to use the cache. That's what it's for.

http://api.drupal.org/api/function/cache_set/6

http://api.drupal.org/api/function/cache_get/6

This allows Drupal to have control over where the data gets stored (database, memcached, APC, etc.)

thanks

igorik - July 4, 2009 - 18:44

thanks yelvington, it looks really great and simply

 
 

Drupal is a registered trademark of Dries Buytaert.