<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Labs &#187; classes</title>
	<atom:link href="http://labs.iamkoa.net/tag/classes/feed/" rel="self" type="application/rss+xml" />
	<link>http://labs.iamkoa.net</link>
	<description>I break it. I fix it. You learn.</description>
	<lastBuildDate>Wed, 22 Sep 2010 19:43:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Three Bitchin&#8217; PHP Classes &#8211; Cache, Last.fm API, Time</title>
		<link>http://labs.iamkoa.net/2010/04/21/three-bitchin-php-classes-cache-last-fm-api-time/</link>
		<comments>http://labs.iamkoa.net/2010/04/21/three-bitchin-php-classes-cache-last-fm-api-time/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 07:11:23 +0000</pubDate>
		<dc:creator>Koa</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[last.fm]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[time]]></category>

		<guid isPermaLink="false">http://labs.iamkoa.net/?p=77</guid>
		<description><![CDATA[There&#8217;s nothing like the feeling that comes with typing a few lines in a PHP application and letting some included classes take care of the dirty work; you know, the filthy stuff like caching, calling the Last.fm API, purifying timestamps, and handling file uploads. (Dirty, right?) All of the following PHP classes are &#8220;works in [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s nothing like the feeling that comes with typing a few lines in a PHP application and letting some included classes take care of the dirty work; you know, the filthy stuff like <strong>caching</strong>, <strong>calling the Last.fm API</strong>, <strong>purifying timestamps</strong>, and <strong>handling file uploads</strong>. (Dirty, right?)</p>
<p>All of the following PHP classes are &#8220;works in progress&#8221;, but hopefully each is mature enough to find its way into your code. Continue reading to view, download, and use each class.</p>
<p><span id="more-77"></span></p>
<hr />
<h2>Cache &#8211; Output &#038; Object Caching</h2>
<p>Though this class is old (three years), it does the trick, and quickly. <a href="http://www.phpclasses.org/browse/package/3807.html" target="_blank">Dragon Bosnjak</a> wrote this gem, allowing for easy caching of webpage content into a directory (and filename) of your choosing.</p>
<h3>Using Cache Class</h3>
<p>Caching your web content is a breeze! Create a new Cache class by specifying a directory to store the cache file, and surround your webpage content in a <code>while</code> loop, like so:</p>
<pre class="brush:php">
$cache = new Cache("../cache/");

while ($cache->save("filename.cache", 30)) {
	// "30" is the number of seconds the content should remain cached.
	// Your cached content goes here...
 }
</pre>
<h3>Download Cache</h3>
<p>You can download and use this golden-nugget of love by visiting <a href="http://www.phpclasses.org/browse/package/3807.html" target="_blank">PHP Classes</a> &#8211; perhaps the most poorly styled/organized website on the internet, but that&#8217;s another post entirely.</p>
<hr />
<h2>Last.fm API</h2>
<p>If you have yet to discover the hotness of using <a href="http://us2.php.net/oop5.magic" target="_blank">&#8220;Magic Methods&#8221; in PHP</a>, I suggest you read up on how to serve up a magic teapot of greatness using PHP5. These Magic Methods are exactly what I used to create this simple Last.fm API class. The result is the ability to quickly make <em>almost</em> any API call without having to continuously fiddle with API key variables, etc.</p>
<h3>Using Last.fm API Class</h3>
<p>After including the LastFm class file, you need to create a new instance of the <code>LastFm()</code> class.</p>
<pre class="brush:php">
// include class file
include_once('../inc/class.lastfm.php');

// initate LastFm class
$lastfm = new LastFm();
$lastfm->key = "1234567890"; // your LastFm API key
</pre>
<p>Then you can call your LastFm API action using the following conversion:</p>
<p>Let&#8217;s say we want to call <a href="http://www.last.fm/api/show?service=299" target="_blank">User.getTopAlbums</a> with the <strong>user</strong> argument set to <code>bob</code> and the <strong>period</strong> argument set to <code>3month</code>:</p>
<pre class="brush:php">
$lastfm->user_getTopAlbums("user=iamkoa","period=3month");
</pre>
<p>The above PHP code will generate the following API call:</p>
<pre class="brush:html">

http://ws.audioscrobbler.com/2.0/?method=user.getTopAlbums&#038;user=bob&#038;period=3month&#038;api_key=1234567890
</pre>
<h3>Let The Class Parse Things For You&#8230;</h3>
<p>The API call will return, for example, the following XML to Array:</p>
<pre class="brush:php">
Array
(
    [lfm] => Array
        (
            [attr] => Array
                (
                    [status] => ok
                )

            [recenttracks] => Array
                (
                    [attr] => Array
                        (
                            [user] => iamkoa
                            [page] => 1
                            [perPage] => 8
                            [totalPages] => 2777
                        )

                    [track] => Array
                        (
                            [0] => Array
                                (
                                    [artist] => Array
                                        (
                                            [value] => Black Eyed Peas
                                            [attr] => Array
                                                (
                                                    [mbid] => d5be5333-4171-427e-8e12-732087c6b78e
                                                )

                                        )
// etc...
</pre>
<p>To make any Last.fm API call, simply replace the <code>.</code> symbol with an <code>_</code> symbol and include any arguments with a <code>"var=bar"</code> pattern. For example, here&#8217;s how to make a couple calls:</p>
<pre class="brush:php">
// call user.getWeeklyTrackChart
$lastfm->user_getWeeklyTrackChart("user=iamkoa");

// call group.getMembers
$lastfm->group_getMembers("group=groupName");

// etc...
</pre>
<h3>Download Last.fm</h3>
<p><a href="http://labs.iamkoa.net/wp-content/uploads/2010/04/php-class-lastfm-04222010.zip">Download and use the Last.fm PHP class</a>. Please post any enhancements to the class here for others to use.</p>
<hr />
<h2>Time</h2>
<p>Time is a bitch. It&#8217;s always against us. In the case of parsing a date string into a human-friendly string, it&#8217;s even more apparent that time sucks. Hopefully this Time class, though not entirely finished, will help with parsing dates.</p>
<h3>Use it</h3>
<p>It&#8217;s really, really simple to use:</p>
<pre class="brush:php">
$time = new Time();
echo $time->timeAgoInWords($date_string);
</pre>
<p>Depending upon when <code>$date_string</code> is relative to the current time, the above code might print one of the following:</p>
<p><code>"26 July, 2:45 PM", "Yesterday morning", "Today", "Last night", or "28 minutes ago", etc...</code></p>
<p>Editing the class is pretty simple, so I&#8217;ll let all you hackers dig into it for details.</p>
<h3>Download Time</h3>
<p><a href="http://labs.iamkoa.net/wp-content/uploads/2010/04/php-class-time-04222010.zip">Download and use the Time PHP class</a>. Please post any enhancements to the class here for others to use.</p>
]]></content:encoded>
			<wfw:commentRss>http://labs.iamkoa.net/2010/04/21/three-bitchin-php-classes-cache-last-fm-api-time/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

