3

我想提取某个用户跟踪列表并显示它。现在我正在执行此操作,但我总是需要连接(通过按下链接)是否有一种方法可以消除此步骤或始终进行身份验证。我已经在 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>';
}
?>
4

1 回答 1

3

您可以像这样创建一个曲目列表:

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 不多,但想法是向以下 URL 发出 HTTP GET) :

$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>';
}

您真的不需要在这里进行身份验证。

于 2013-02-04T14:59:20.697 回答