Grab and Cache an URL with Xaraya

Xaraya is built to be an extensible system. One module may well help your development with twenty other modules along the way. Much has been documented and spoken about the hook system where one module may well provide output for others, ala the comments system. However, Xaraya has natural tools as well to decrease development time which are simple API calls that are reusable. One of the lesser known tools but definitely helpful is the ability to grab a file from an Internet location.

Grabbing a file could be used for many different purposes. For instance, syndication of rss, rdf, xml, all require you to grab a file from somewhere and then process it. You may well want to cache the file as well in order to relieve bandwidth requirements as well. Xaraya can grab these files naturally from a simple API call to the base module:

Let's look at this call a little closer. The first line of the function call is setting the $filedata variable to be the base_userapi_getfile function output. Whenever you are calling xarModAPIFunc you are basically either checking true or false of the function based on arguments, or you are getting the output of the processing. This holds true with any API call in Xaraya.

We are also sending arguments to the getfile API to tell the function what to do with the output. The first argument is simple, url is the url that you want to grab from Internet. It could be anything from an RSS file to an HTML file. It also does not necessarily have to be from another website, as you could grab this locally. Cached and Cachedir arguments tell Xaraya whether or not you want to store a copy of the file, and where to store it. For instance, if your module was checking to see if a page had been updated, you might not want to store a copy of the file. The Refresh argument tells Xaraya how long to store the file for before grabbing a new copy. The next argument is the extension, which in this case is php, but could well be xml or html, or whatever extension that you would want to use for the storage of the file. Finally, we tell Xaraya whether or not to save an archive of the file.

This simple function can do away with several lines of code in several different modules that you may be writing. It is reusable, and it speeds development, which after all is the cornerstone of Xaraya.

Print | Related