我想拉出某个用户曲目列表并显示它。现在我正在为此配备人员,但我总是需要连接(通过按链接)是否有办法消除此步骤或始终进行身份验证的方法。我已经在 soundcloud api 上重新搜索了这个,但没有成功,我不明白为什么在访问公共数据时需要身份验证。
<?php
session_start();
//session_destroy();
require 'Soundcloud.php';
$soundcloud = new Services_Soundcloud('MY_CLIENT_ID', 'MY_SECRET', 'http://www.ericmlt.com/MarcKane/soundcloud/');
$soundcloud->setDevelopment(FALSE);
$authURL = $soundcloud->getAuthorizeUrl();
echo "<a href='$authURL'>Connect to Soundcloud</a>";
//Attempt to get token from Session first
//Set the token otherwise..
try {
$accessToken = $soundcloud->accessToken($_GET['code']);
if(!isset($_SESSION['token'])){
$_SESSION['token'] = $accessToken['access_token'] ;
} else {
$soundcloud->setAccessToken($_SESSION['token']);
}
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
exit($e->getMessage());
}
try {
$tracks = json_decode($soundcloud->get('tracks', array('user_id' => '857348')), TRUE);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
exit($e->getMessage());
}
foreach ($tracks as $track){
$trackID = $track['id'];
echo '<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F'.$trackID.'"></iframe>';
}
?>
您可以像这样创建一个曲目列表:
echo '<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=http://api.soundcloud.com/users/857348"></iframe>';
否则,如果您需要检索曲目列表并为每个曲目创建小部件(不真正推荐),您可以使用这个(注意我写的不是很多 PHP,但想法是将 HTTP GET 发布到以下 URL):
$tracks_json = file_get_contents('http://api.soundcloud.com/users/857348/tracks.json?client_id=YOUR_CLIENT_ID');
$tracks = json_decode($tracks_json);
foreach ($tracks as $track){
$trackID = $track->id;
echo '<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F'.$trackID.'"></iframe>';
}
您实际上不需要在此处进行身份验证。