2010-04-26 117 views
3

我正在構建網站,我需要從我的soundcloud帳戶查詢我的最後兩條曲目,並將它們顯示在我的網頁上。 我已經閱讀過Soundcloud API文檔,但它似乎很模糊,遠不及我的範圍。 我已經安裝了使用API​​和Oauth的PHP庫,並設置了我的SoundCloud應用程序以獲取我的使用者密鑰,但我無法啓動OAuth會話。我使用librarySoundcloud API,PHP和OAuth

我需要從我的Soundcloud帳戶中檢索最後2首曲目。在我需要庫中的文件(soundcloud.php和oauth.php)之後,我需要設置四個參數:$ consumer_key,$ consumer_secret,$ callback_url,$ tmp_path。

我已經有我的密鑰和我的可寫緩存文件夾。我不知道我的回調網址是什麼。另外,我必須說我找不到任何有效的示例代碼,所以我甚至無法開始寫任何東西。如此封鎖!

有什麼辦法可以在不調用另一個窗口的情況下使OAuth進程自動化,所以我的OAuth令牌在我的PHP腳本中被請求?

我想知道如果也許你可以給我一些示例代碼來做到這一點。 這將是偉大的!

+0

你能提供更多的信息嗎?你期望什麼,你取而代之的是什麼?也許一些代碼示例。 – Ikke 2010-04-26 17:10:06

+0

@Ikke嗨!感謝您回答如此之快。我需要從我的Soundcloud帳戶中檢索最後2首曲目。在我需要庫中的文件(soundcloud.php和oauth.php)之後,我需要設置四個參數:$ consumer_key,$ consumer_secret,$ callback_url,$ tmp_path。我已經有我的密鑰和我的可寫緩存文件夾。我不知道我的回調網址是什麼。 另外,我必須說我找不到任何示例代碼,所以我甚至無法開始寫任何東西。如此封鎖! 任何建議將不勝感激。 – fedeisas 2010-04-26 17:17:48

+0

將此添加到問題中以改善問題。 – Ikke 2010-04-26 17:48:57

回答

2

這可能對您有用。登錄後,它會將我最喜歡的曲目嵌入到頁面中。 您可以更改$ favs來加載您自己的歌曲,而不是您的收藏夾。

另請注意,我的config.php包含了我的consumer_key,consumer_secret和我的callback_url。

$callback_url = 'http://localhost/soundcloud';

你想讓它等於地方,你的PHP腳本。

<?php 
    require_once ('php-soundcloud/mptre-php-soundcloud-644bb0e/oauth.php'); 
    require_once ('php-soundcloud/mptre-php-soundcloud-644bb0e/soundcloud.php'); 
    require_once ('config.php'); 

session_start(); 

// Clear the session i.e delete all stored tokens. 
if (isset($_GET['logout'])) { 
    session_destroy(); 
} 

// Variables used for verifying the status of the "OAuth dance". 
$oauth_token = (isset($_GET['oauth_verifier'])) 
    ? $_GET['oauth_verifier'] 
    : ((isset($_SESSION['oauth_access_token'])) ? $_SESSION['oauth_access_token'] : NULL); 
$oauth_request_token = (isset($_SESSION['oauth_request_token'])) 
    ? $_SESSION['oauth_request_token'] 
    : NULL; 
$oauth_request_token_secret = (isset($_SESSION['oauth_request_token_secret'])) 
    ? $_SESSION['oauth_request_token_secret'] 
    : NULL; 

if (isset($oauth_token) && isset($oauth_request_token) && isset($oauth_request_token_secret)) { 
    // Retreive access tokens if missing. 
    if (!isset($_SESSION['oauth_access_token']) && !isset($_SESSION['oauth_access_token_secret'])) { 
     $soundcloud = new Soundcloud(
      $consumer_key, 
      $consumer_secret, 
      $_SESSION['oauth_request_token'], 
      $_SESSION['oauth_request_token_secret'] 
     ); 
     $token = $soundcloud->get_access_token($oauth_token); 
     $_SESSION['oauth_access_token'] = $token['oauth_token']; 
     $_SESSION['oauth_access_token_secret'] = $token['oauth_token_secret']; 
    } 

    // Construct a fully authicated connection with SoundCloud. 
    $soundcloud = new Soundcloud(
     $consumer_key, 
     $consumer_secret, 
     $_SESSION['oauth_access_token'], 
     $_SESSION['oauth_access_token_secret'] 
    ); 

    // Get basic info about the authicated visitor. 
    $me = $soundcloud->request('me'); 
    $me = new SimpleXMLElement($me); 
    $me = get_object_vars($me); 

    // Get some embedding code for favs 
    $favs = $soundcloud->request('http://api.soundcloud.com/users/'.$me['id'].'/favorites/'); 
    $favs = new SimpleXMLElement($favs); 

} else { 
    // This is the first step in the "OAuth dance" where we ask the visitior to authicate himself. 
    $soundcloud = new Soundcloud($consumer_key, $consumer_secret); 
    $token = $soundcloud->get_request_token($callback_url); 

    $_SESSION['oauth_request_token'] = $token['oauth_token']; 
    $_SESSION['oauth_request_token_secret'] = $token['oauth_token_secret']; 

    $login = $soundcloud->get_authorize_url($token['oauth_token']); 
} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <title>SoundCloud PHP API Wrapper</title> 
    <meta name="author" content="Anton Lindqvist" /> 
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/reset/reset-min.css" /> 
    <link rel="stylesheet" type="text/css" href="assets/css/style.css" /> 
</head> 
<body> 
    <div id="wrapper"> 
     <div id="content"> 
      <?php if (isset($me)): ?> 
       <a class="logout" href="?logout=true">logout</a> 
      <?php endif; ?> 
      <div id="header"> 
       <h1>SoundCloud PHP API Wrapper</h1> 
      </div> 
      <?php if (isset($login)): ?> 
      <h2>What is this?</h2> 
      <p>This is a basic demo</p> 
      <h2>How to start?</h2> 
      <p><a class="button" href="<?php echo $login; ?>">login with your SoundCloud account</a></p> 
      <?php elseif (isset($me)): ?> 
       <div id="profile"> 
        <h2> 
        <a href="<?php echo $me['permalink-url']; ?>"><?php echo $me['permalink']; ?></a> 
        </h2> 
       </div> 
       <div class="clear"></div> 

       <div id="favs"> 
       <?php 
        if (isset($favs)){ 

        foreach($favs->track as $fav){ 
         $permalink_url = $fav->{'permalink-url'}; 
         $permalink_url = urlencode($permalink_url); 

         $f = simplexml_load_file('http://soundcloud.com/oembed?url='.$permalink_url); 
         echo $f->html; 
        } 

        } else { 
        echo "fail"; 
        } 
       ?> 
       </div> 
      <?php endif; ?> 
     </div> 
    </div> 
</body> 
</html> 

另外請注意,我是一個PHP的菜鳥,使用此API的第一次...所以我的能力不伸過去,這針對的時刻。大多數這是從你使用的php包裝庫附帶的演示中「借用」的。

希望它幫助雖然:)

ps。不確定是否有辦法在未調用其他窗口的情況下自動化OAuth進程。

+0

另外我剛剛注意到登錄/註銷需要2次嘗試才能成功:( – colinjwebb 2010-07-08 10:36:48