如何使用的SoundCloud API用户显示playback_count [英] How to display playback_count for a user using the SoundCloud API
问题描述
我想显示使用的SoundCloud API特定的SoundCloud用户的总曲目的意见(playback_count)。
I am trying to display the total track views ("playback_count") for a specific SoundCloud user using the SoundCloud API.
根据API文档,我得到使用下面的函数调用的信息:
According to the API documentation I get the info using the below function call:
http://api.soundcloud.com/tracks/13158665.json?client_id=YOUR_CLIENT_ID
这是很好,因为它显示的号码13158665。
This is fine because it displays the number "13158665".
这个数字是什么?难道是的TrackID?
What is this number? Is it the trackid?
我需要获得playback_count使用用户的用户名的用户。
I need to get the "playback_count" for a user using the users username.
我尝试使用这种从用户名获取用户名:
I tried getting the UserId from the Username using this:
$soundcloud_playsAPI = "MY_SOUNDCLOUD_API_KEY";
/* Get the SoundCloud UserId from the username */
$json = wp_remote_get("http://api.soundcloud.com/users/jwagener.json?client_id=".$soundcloud_playsAPI);
$soundcloudData = json_decode($json['body'], true);
$soundcloud_userid = $soundcloudData['id'];
这将返回用户名:3207181
This returns the UserId: 3207181
现在我试图替代该响应到previous URL获得playback_count,但它失败了。
Now I tried to substitute that response into the previous URL to get the "playback_count" but it failed.
$json = wp_remote_get("http://api.soundcloud.com/tracks/3207181.json?client_id=".$soundcloud_playsAPI);
$soundcloudPlaysData = json_decode($json['body'], true);
echo $soundcloudPlaysData['playback_count'];
任何指导,将大大AP preciated。
Any guidance would be greatly appreciated.
感谢。
推荐答案
下面是完整的解决方案:
Here is the full solution:
function listPlays()
{SC.initialize({ client_id: 'YOUR ID HERE'});
// Get the SoundCloud UserId from the username
var userName="jwagener";
SC.get("/users/"+userName, function (users)
{console.log(users.id);
var myId=users.id;
getTracks(myId);
});
var getTracks=function (myId)
{var totalPlays=0;
SC.get("/users/"+myId+"/tracks", function(getTracks)
{for (var key in getTracks) //get each track and look at it's playback_count
{console.log(getTracks[key].title+" "+getTracks[key].playback_count);
totalPlays+=getTracks[key].playback_count; //add the playback count for this track to the total
}
console.log("Total Plays for all tracks: "+totalPlays);
});
};
};
这篇关于如何使用的SoundCloud API用户显示playback_count的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!