last.fmから最新の再生履歴を配列として取得するphpスクリプト

| | Comments (0) | TrackBacks (0)
last.fm から自分の再生履歴を配列として取得するphpスクリプトのサンプルを以下に示す。


<?php

// --
// VERSION CHECK
// --
if( version_compare(phpversion(), '5.0', '<') ) {
    echo "本スクリプトはPHPのVersion5.0以降でのみ動作します\n";
    exit;
}

// --
// LOAD XML
// --
$_lastfmName = 'mahata';
$_targetUrl = "http://ws.audioscrobbler.com/1.0/user/{$_lastfmName}/recenttracks.xml";
$_xmlObj = simplexml_load_file($_targetUrl);

// --
// MAKE ARRAY
// --
$_playList = array();
foreach( $_xmlObj as $_xmlObj_ ) {
    $_tArray = array();
    $_tArray['artist']  = (string)$_xmlObj_->artist;
    $_tArray['name']    = (string)$_xmlObj_->name;
    $_tArray['album']   = (string)$_xmlObj_->album;
    $_tArray['date']    = date('Y/m/d H:i:s', (int)strtotime($_xmlObj_->date));

    $_playList[] = $_tArray;
}

// --
// DUMP ARRAY
// --
print_r( $_playList );

?>


ここで、$_lastfmNameに任意のIDを設定すればよい。
実行結果は次のようになる。


$ php playlist2array.php
Array
(
    [0] => Array
        (
            [artist] => Surface
            [name] => クセになりそうだ
            [album] => resurface
            [date] => 2007/10/03 14:42:00
        )

    [1] => Array
        (
            [artist] => Surface
            [name] => ハニカムハニ
            [album] => resurface
            [date] => 2007/10/03 14:37:00
        )

    [2] => Array
        (
            [artist] => Surface
            [name] => WAIT!
            [album] => resurface
            [date] => 2007/10/03 14:34:00
        )

    [3] => Array
        (
            [artist] => Surface
            [name] => Re:START
            [album] => Re:START
            [date] => 2007/10/03 14:30:00
        )

    [4] => Array
        (
            [artist] => Surface
            [name] => ちりつもたれつ
            [album] => resurface
            [date] => 2007/10/03 14:26:00
        )

    [5] => Array
        (
            [artist] => Surface
            [name] => ワクチン
            [album] =>
            [date] => 2007/10/03 14:20:00
        )

    [6] => Array
        (
            [artist] => Surface
            [name] => 夢の続きへ
            [album] =>
            [date] => 2007/10/03 14:15:00
        )

    [7] => Array
        (
            [artist] => LINDBERG
            [name] => Brand New Day
            [album] => XII
            [date] => 2007/10/03 14:09:00
        )

    [8] => Array
        (
            [artist] => LINDBERG
            [name] => アタシは磨けば光るダイアモンドなのに
            [album] => XII
            [date] => 2007/10/03 14:05:00
        )

    [9] => Array
        (
            [artist] => LINDBERG
            [name] => 自転車に初めて乗れた日
            [album] => XII
            [date] => 2007/10/03 13:59:00
        )

)

0 TrackBacks

Listed below are links to blogs that reference this entry: last.fmから最新の再生履歴を配列として取得するphpスクリプト.

TrackBack URL for this entry: http://mahata.net/movabletype/mt-tb.cgi/2979

Leave a comment

About this Entry

This page contains a single entry by mahata published on October 3, 2007 11:41 PM.

google-code-prettifyでソースコードのシンタックスハイライト was the previous entry in this blog.

ドラフト会議 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.