如何使用SoundCloud API返回Array循环浏览SoundCloud轨道列表

时间:2013-03-13 01:19:46

标签: api loops iterator soundcloud

我正在使用SoundCloud API尝试获取用户曲目播放的次数。这将是所有曲目合计播放的总金额。

我可以从Soundcloud API获取一个返回的数组,该数组显示所有可用的用户曲目。

我需要遍历所有这些曲目并添加所有“[playback_count]”。这将给出我需要的总数。

我只是不知道如何遍历数组以获取信息并获取并显示最终结果。

非常感谢任何帮助。

这是返回的数组 - 由于代码很多,我删除了一些方面。

Array
(
    [0] => Array
        (
            [kind] => track
            [id] => 81677429
            [created_at] => 2013/03/03 22:32:32 +0000
            [user_id] => 7160186
            [duration] => 365773
            [commentable] => 1
            [state] => finished
            [original_content_size] => 15046155
            [sharing] => public
            [tag_list] => nari & milani martindhamen benny benassi steve angello laidback luke mightydubkatz robin s amanda wilson justanothergroove atom satisfaction showmelove loveonmymind mass 2012 2013 house progressive electro bootleg remix mashup
            [permalink] => just-another-groove-one-step
            [streamable] => 1
            [embeddable_by] => all
            [downloadable] => 1
            [purchase_url] => 
            [label_id] => 
            [purchase_title] => 
            [genre] => Progressive House
            [title] => Just Another Groove, One Step Atom, Satisfaction, Show Me Love, Love On My Mind (MASS BOOTLEG)
            [description] => Nari & Milani vs. Martin Dhamen, Benny Benassi, Steve Angello, Laidback Luke, Mighty Dub Katz featuring The Biz and Robin S, Amanda Wilson -  Just Another Groove, One Step Atom, Satisfaction, Show Me Love, Love On My Mind (MASS BOOTLEG)
            [label_name] => 
            [release] => 
            [track_type] => 
            [key_signature] => 
            [isrc] => 
            [video_url] => 
            [bpm] => 
            [release_year] => 
            [release_month] => 
            [release_day] => 
            [original_format] => mp3
            [license] => all-rights-reserved
            [uri] => http://api.soundcloud.com/tracks/81677429
            [user] => Array
                (
                    [id] => 7160186
                    [kind] => user
                    [permalink] => mass-official
                    [username] => mass official
                    [uri] => http://api.soundcloud.com/users/7160186
                    [permalink_url] => http://soundcloud.com/mass-official
                    [avatar_url] => http://i1.sndcdn.com/avatars-000029524162-jdiyn4-large.jpg?45575d7
                )

            [permalink_url] => http://soundcloud.com/mass-official/just-another-groove-one-step
            [artwork_url] => http://i1.sndcdn.com/artworks-000042075610-suv3cx-large.jpg?45575d7
            [waveform_url] => http://w1.sndcdn.com/qWZAqcPzSqdW_m.png
            [stream_url] => http://api.soundcloud.com/tracks/81677429/stream
            [download_url] => http://api.soundcloud.com/tracks/81677429/download
            [playback_count] => 668
            [download_count] => 132
            [favoritings_count] => 12
            [comment_count] => 7
            [attachments_uri] => http://api.soundcloud.com/tracks/81677429/attachments
        )

    [1] => Array
        (
            [kind] => track
            [id] => 64330580
            [created_at] => 2012/10/22 06:33:05 +0000
            [user_id] => 7160186
            [duration] => 4704989
            [commentable] => 1
            [state] => finished
            [original_content_size] => 188185127
            [sharing] => public
            [tag_list] => 
            [permalink] => mass-summer-party-mix-2012-1
            [streamable] => 1
            [embeddable_by] => all
            [downloadable] => 1
            [purchase_url] => 
            [label_id] => 
            [purchase_title] => 
            [genre] => Dance-pop, House
            [title] => MASS SUMMER PARTY MIX 2012
            [description] => 01-Will.I.Am feat. Eva Simons - This Is Love
            [label_name] => 
            [release] => 
            [track_type] => 
            [key_signature] => 
            [isrc] => 
            [video_url] => 
            [bpm] => 
            [release_year] => 
            [release_month] => 
            [release_day] => 
            [original_format] => mp3
            [license] => all-rights-reserved
            [uri] => http://api.soundcloud.com/tracks/64330580
            [user] => Array
                (
                    [id] => 7160186
                    [kind] => user
                    [permalink] => mass-official
                    [username] => mass official
                    [uri] => http://api.soundcloud.com/users/7160186
                    [permalink_url] => http://soundcloud.com/mass-official
                    [avatar_url] => http://i1.sndcdn.com/avatars-000029524162-jdiyn4-large.jpg?45575d7
                )

            [permalink_url] => http://soundcloud.com/mass-official/mass-summer-party-mix-2012-1
            [artwork_url] => http://i1.sndcdn.com/artworks-000032637806-efhtcw-large.jpg?45575d7
            [waveform_url] => http://w1.sndcdn.com/bVg89jcTqVDF_m.png
            [stream_url] => http://api.soundcloud.com/tracks/64330580/stream
            [download_url] => http://api.soundcloud.com/tracks/64330580/download
            [playback_count] => 5416
            [download_count] => 3120
            [favoritings_count] => 95
            [comment_count] => 10
            [attachments_uri] => http://api.soundcloud.com/tracks/64330580/attachments
        )

    [2] => Array
        (
            [kind] => track
            [id] => 59035163
            [created_at] => 2012/09/07 15:30:13 +0000
            [user_id] => 7160186
            [duration] => 426645
            [commentable] => 1
            [state] => finished
            [original_content_size] => 17065398
            [sharing] => public
            [tag_list] => avicii nicky romero coldplay mass nicktim everyteardropisawaterfall 2012 mix house progressive electronic electronica bootleg mashup
            [permalink] => avicii-nicky-romero-ft-1
            [streamable] => 1
            [embeddable_by] => all
            [downloadable] => 1
            [purchase_url] => 
            [label_id] => 
            [purchase_title] => 
            [genre] => Progressive House
            [title] => Avicii & Nicky Romero ft. Coldplay - Nicktim Vs. Every Teardrop Is A Waterfall (Mass Bootleg)
            [description] => 
            [label_name] => 
            [release] => 
            [track_type] => 
            [key_signature] => 
            [isrc] => 
            [video_url] => 
            [bpm] => 
            [release_year] => 
            [release_month] => 
            [release_day] => 
            [original_format] => mp3
            [license] => all-rights-reserved
            [uri] => http://api.soundcloud.com/tracks/59035163
            [user] => Array
                (
                    [id] => 7160186
                    [kind] => user
                    [permalink] => mass-official
                    [username] => mass official
                    [uri] => http://api.soundcloud.com/users/7160186
                    [permalink_url] => http://soundcloud.com/mass-official
                    [avatar_url] => http://i1.sndcdn.com/avatars-000029524162-jdiyn4-large.jpg?45575d7
                )

            [permalink_url] => http://soundcloud.com/mass-official/avicii-nicky-romero-ft-1
            [artwork_url] => http://i1.sndcdn.com/artworks-000029888965-2kigan-large.jpg?45575d7
            [waveform_url] => http://w1.sndcdn.com/iJ3jS6UcvgH0_m.png
            [stream_url] => http://api.soundcloud.com/tracks/59035163/stream
            [download_url] => http://api.soundcloud.com/tracks/59035163/download
            [playback_count] => 2786
            [download_count] => 728
            [favoritings_count] => 38
            [comment_count] => 11
            [attachments_uri] => http://api.soundcloud.com/tracks/59035163/attachments
        )

    [3] => Array
        (
            [kind] => track
            [id] => 56192626
            [created_at] => 2012/08/13 19:58:55 +0000
            [user_id] => 7160186
            [duration] => 309616
            [commentable] => 1
            [state] => finished
            [original_content_size] => 12380020
            [sharing] => public
            [tag_list] => rihanna miami2ibiza wherehaveyoubeen mass bootleg 2012 progressive swedish house mafia
            [permalink] => swedish-house-mafia-ft
            [streamable] => 1
            [embeddable_by] => all
            [downloadable] => 1
            [purchase_url] => 
            [label_id] => 
            [purchase_title] => 
            [genre] => Progressive House
            [title] => Swedish House Mafia Ft. Rihanna - Where Have You Been, Miami 2 Ibiza (Mass Bootleg)
            [description] => 
            [label_name] => 
            [release] => 
            [track_type] => 
            [key_signature] => 
            [isrc] => 
            [video_url] => 
            [bpm] => 
            [release_year] => 
            [release_month] => 
            [release_day] => 
            [original_format] => mp3
            [license] => all-rights-reserved
            [uri] => http://api.soundcloud.com/tracks/56192626
            [user] => Array
                (
                    [id] => 7160186
                    [kind] => user
                    [permalink] => mass-official
                    [username] => mass official
                    [uri] => http://api.soundcloud.com/users/7160186
                    [permalink_url] => http://soundcloud.com/mass-official
                    [avatar_url] => http://i1.sndcdn.com/avatars-000029524162-jdiyn4-large.jpg?45575d7
                )

            [permalink_url] => http://soundcloud.com/mass-official/swedish-house-mafia-ft
            [artwork_url] => http://i1.sndcdn.com/artworks-000028404589-qmmquq-large.jpg?45575d7
            [waveform_url] => http://w1.sndcdn.com/5HwZSU9IA03i_m.png
            [stream_url] => http://api.soundcloud.com/tracks/56192626/stream
            [download_url] => http://api.soundcloud.com/tracks/56192626/download
            [playback_count] => 3567
            [download_count] => 891
            [favoritings_count] => 66
            [comment_count] => 20
            [attachments_uri] => http://api.soundcloud.com/tracks/56192626/attachments
        )

    [4] => Array
        (
            [kind] => track
            [id] => 55370840
            [created_at] => 2012/08/06 13:43:46 +0000
            [user_id] => 7160186
            [duration] => 352943
            [commentable] => 1
            [state] => finished
            [original_content_size] => 14112438
            [sharing] => public
            [tag_list] => mass progressive house 2012 mashup timbaland basto bootleg remix keri hilson thewayiare
            [permalink] => the-way-i-are-mass-mashup
            [streamable] => 1
            [embeddable_by] => all
            [downloadable] => 1
            [purchase_url] => 
            [label_id] => 
            [purchase_title] => 
            [genre] => Progressive House
            [title] => Timbaland ft. Keri Hilson vs Kylie Minogue - Put Your Hands Up, The Way i are (Mass Mashup)
            [description] => Tracks:
Kylie Minogue - Put Your Hands Up (If You Feel Love) (Basto's Major Mayhem US Instrumental)
Timberland Featuring Keri Hilson - The Way I Are
            [label_name] => 
            [release] => 
            [track_type] => 
            [key_signature] => 
            [isrc] => 
            [video_url] => 
            [bpm] => 
            [release_year] => 
            [release_month] => 
            [release_day] => 
            [original_format] => mp3
            [license] => all-rights-reserved
            [uri] => http://api.soundcloud.com/tracks/55370840
            [user] => Array
                (
                    [id] => 7160186
                    [kind] => user
                    [permalink] => mass-official
                    [username] => mass official
                    [uri] => http://api.soundcloud.com/users/7160186
                    [permalink_url] => http://soundcloud.com/mass-official
                    [avatar_url] => http://i1.sndcdn.com/avatars-000029524162-jdiyn4-large.jpg?45575d7
                )

            [permalink_url] => http://soundcloud.com/mass-official/the-way-i-are-mass-mashup
            [artwork_url] => http://i1.sndcdn.com/artworks-000027981094-tnahdz-large.jpg?45575d7
            [waveform_url] => http://w1.sndcdn.com/A2SE6kUb06MP_m.png
            [stream_url] => http://api.soundcloud.com/tracks/55370840/stream
            [download_url] => http://api.soundcloud.com/tracks/55370840/download
            [playback_count] => 2270
            [download_count] => 621
            [favoritings_count] => 28
            [comment_count] => 17
            [attachments_uri] => http://api.soundcloud.com/tracks/55370840/attachments
        )

    [5] => Array
        (
            [kind] => track
            [id] => 49905943
            [created_at] => 2012/06/16 19:31:15 +0000
            [user_id] => 7160186
            [duration] => 403588
            [commentable] => 1
            [state] => finished
            [original_content_size] => 16137458
            [sharing] => public
            [tag_list] => "eric prydz" "dirty south" "those usual suspects" erikhecht "walking alone" 2night 2012 mass mashup "progressive house"
            [permalink] => walking-alone-2night-mass-1
            [streamable] => 1
            [embeddable_by] => all
            [downloadable] => 1
            [purchase_url] => 
            [label_id] => 
            [purchase_title] => 
            [genre] => Progressive House
            [title] => Eric Prydz, Dirty South & Those Usual Suspects Ft. Erik Hecht - Walking Alone 2NIGHT (Mass Mashup)
            [description] => 
            [label_name] => 
            [release] => 
            [track_type] => 
            [key_signature] => 
            [isrc] => 
            [video_url] => 
            [bpm] => 
            [release_year] => 
            [release_month] => 
            [release_day] => 
            [original_format] => mp3
            [license] => all-rights-reserved
            [uri] => http://api.soundcloud.com/tracks/49905943
            [user] => Array
                (
                    [id] => 7160186
                    [kind] => user
                    [permalink] => mass-official
                    [username] => mass official
                    [uri] => http://api.soundcloud.com/users/7160186
                    [permalink_url] => http://soundcloud.com/mass-official
                    [avatar_url] => http://i1.sndcdn.com/avatars-000029524162-jdiyn4-large.jpg?45575d7
                )

            [permalink_url] => http://soundcloud.com/mass-official/walking-alone-2night-mass-1
            [artwork_url] => http://i1.sndcdn.com/artworks-000025156708-j3k92q-large.jpg?45575d7
            [waveform_url] => http://w1.sndcdn.com/LtSQavuMGFiI_m.png
            [stream_url] => http://api.soundcloud.com/tracks/49905943/stream
            [download_url] => http://api.soundcloud.com/tracks/49905943/download
            [playback_count] => 4982
            [download_count] => 911
            [favoritings_count] => 64
            [comment_count] => 53
            [attachments_uri] => http://api.soundcloud.com/tracks/49905943/attachments
        )

    [11] => Array
        (
            [kind] => track
            [id] => 32861896
            [created_at] => 2012/01/09 14:05:01 +0000
            [user_id] => 7160186
            [duration] => 379886
            [commentable] => 1
            [state] => finished
            [original_content_size] => 15189753
            [sharing] => public
            [tag_list] => prty a lmfao partyrock partyrockanm 2012 mass mashup rayfoxx the thetrumpeter house latin electronic
            [permalink] => lmfao-ray-foxx-party-rock
            [streamable] => 1
            [embeddable_by] => all
            [downloadable] => 1
            [purchase_url] => 
            [label_id] => 
            [purchase_title] => 
            [genre] => Mashup/Bootleg
            [title] => LMFAO-Ray Foxx -  Party Rock Trumpeter (Mass Mashup)
            [description] => 
            [label_name] => 
            [release] => 
            [track_type] => 
            [key_signature] => 
            [isrc] => 
            [video_url] => 
            [bpm] => 
            [release_year] => 
            [release_month] => 
            [release_day] => 
            [original_format] => mp3
            [license] => all-rights-reserved
            [uri] => http://api.soundcloud.com/tracks/32861896
            [user] => Array
                (
                    [id] => 7160186
                    [kind] => user
                    [permalink] => mass-official
                    [username] => mass official
                    [uri] => http://api.soundcloud.com/users/7160186
                    [permalink_url] => http://soundcloud.com/mass-official
                    [avatar_url] => http://i1.sndcdn.com/avatars-000029524162-jdiyn4-large.jpg?45575d7
                )

            [permalink_url] => http://soundcloud.com/mass-official/lmfao-ray-foxx-party-rock
            [artwork_url] => http://i1.sndcdn.com/artworks-000017156126-hlomgx-large.jpg?45575d7
            [waveform_url] => http://w1.sndcdn.com/nVXX8DZRwfBF_m.png
            [stream_url] => http://api.soundcloud.com/tracks/32861896/stream
            [download_url] => http://api.soundcloud.com/tracks/32861896/download
            [playback_count] => 2060
            [download_count] => 463
            [favoritings_count] => 24
            [comment_count] => 12
            [attachments_uri] => http://api.soundcloud.com/tracks/32861896/attachments
        )

)

这是我用来获取数组的代码 - 使用WordPress

<?php
/* Now get the tracks information */
$json = wp_remote_get("http://api.soundcloud.com/users/USERID/tracks.json?client_id=CLIENT_ID");

$soundcloudData = json_decode($json['body'], true);

print "<pre>";
print_r($soundcloudData);
print "</pre>";
?>

解决方案

我想我得到了它的工作。我不得不略微改变代码。

$json = wp_remote_get("http://api.soundcloud.com/users/7160186/tracks.json?client_id={MY_CLIENT_ID}");
$soundcloudData = json_decode($json['body'], true);

$totalPlays = 0;

foreach ($soundcloudData as $track) {
    $totalPlays += $track['playback_count'];
}
print_r($totalPlays);

这返回了数字 117427

1 个答案:

答案 0 :(得分:1)

以下内容应该有效:

<?php
$json = wp_remote_get("http://api.soundcloud.com/users/2/tracks.json?client_id=YOUR_CLIENT_ID");
$soundcloudData = json_decode($json);
$totalPlays = 0;

foreach ($soundcloudData as $track) {
    $totalPlays += $track->playback_count;
}

print_r($totalPlays);
?>