Programmed Under The Influence
Parsing RSS feeds using SimpleXML

I’m was working with Last.fm’s API service to post user’s recent tracks on one of my sites. I wanted a lighter solution to using their REST Request method so I opted to use their RSS feeds instead. In the past I used either Magpie or SimplePie to parse the RSS for easy access, but I didn’t want the overhead of both these services so I did some research and found something of use courtesy of Stuart Herbert’s article, “Using SimpleXML To Parse RSS Feeds.”

I’ll give a brief tutorial on how I used Stuart’s tutorial to display my 10 most recent tracks from Last.fm:


// Get the RSS URL and plug in your ~username~
$lastfm_feed = "http://ws.audioscrobbler.com/1.0/user/~username~/recenttracks.rss";

// Turn the feed into a string and load it using PHP's simpleXML function
$raw_feed = file_get_contents($lastfm_feed);
$xml = simplexml_load_string($raw_feed);

// Next throw the results into a foreach loop to display the results
// I'll just display all the values you can print out
foreach ($xml->channel->item as $item) {
    echo $item->title; // Track title
    echo $item->link; // Link back to the track on Last.fm
    echo $item->pubDate; // Date the track was played
    echo $item->guid; // Unique identifier
    echo $item->description; // Links back to the artist's page
}
Updating your Jaiku status revisited

After failing miserably trying to find a way to successfully updating my Jaiku status externally, I decided to give up. It has seemed like I had tapped all the resources online describing how you do it, but I just couldn’t get it to work on my end. My first attempt was following Jaiku’s instructions in using OAuth to connect to the site, but I soon found out that the callback URL pass doesn’t actually “call back” to your site. Anyway, I started trying my somewhat limited knowledge of cURL until I finally struck gold. Here’s what I got:


$username = "your_username";
$personal_key = "your_personal_key"; // Get yours here
$status = "your_status_message";
$icon = 300; // For the whole list go here
postToJaiku($username, $personal_key, $status, $icon);

function postToJaiku($username, $personal_key, $status, $icon = 300) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.jaiku.com/json");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "user=" . urlencode($username) .
"&personal_key=" . urlencode($personal_key) .
"&method=post&message=" . urlencode($message) . "&icon=" . $icon);

curl_exec($ch);
curl_close($ch);
}