SoundCloudAPI、PHP和OAuth

SoundCloudAPI、PHP和OAuth,php,xml,api,oauth,soundcloud,Php,Xml,Api,Oauth,Soundcloud,我正在建立一个网站,我需要从我的soundcloud帐户查询我的最后两首曲目,并将它们显示在我的页面上。 我已经阅读了SoundCloudAPI文档,但它似乎很模糊,离我的目标还很远。 我已经安装了使用API和Oauth的PHP库,并设置了SoundCloud应用程序来获取用户密钥,但我无法启动Oauth会话 我在用这个 我需要从我的Soundcloud帐户中检索最后2首曲目。在我需要库中的文件(soundcloud.php和oauth.php)之后,我需要设置四个参数:$consumer\u

我正在建立一个网站,我需要从我的soundcloud帐户查询我的最后两首曲目,并将它们显示在我的页面上。 我已经阅读了SoundCloudAPI文档,但它似乎很模糊,离我的目标还很远。 我已经安装了使用API和Oauth的PHP库,并设置了SoundCloud应用程序来获取用户密钥,但我无法启动Oauth会话

我在用这个

我需要从我的Soundcloud帐户中检索最后2首曲目。在我需要库中的文件(soundcloud.php和oauth.php)之后,我需要设置四个参数:$consumer\u key、$consumer\u secret、$callback\u url、$tmp\u path

我已经有了密钥和可写缓存文件夹。我不知道我的回调url是什么。此外,我必须说,我找不到任何有效的示例代码,所以我甚至不能开始编写任何东西。太堵了

有没有办法在不调用另一个窗口的情况下自动化OAuth进程,从而在PHP脚本中请求OAuth令牌

我想知道你是否可以给我一些这样做的示例代码。
那太好了

这可能对您有用。登录后,它会将我最喜欢的曲目嵌入页面。 您可以将$favs更改为加载您自己的歌曲,而不是您最喜爱的歌曲

还要注意,my config.php包括我的消费者密钥、消费者密钥和回调url

$callback\u url='1http://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>

SoundCloud PHP API包装器
SoundCloud PHP API包装器
这是什么?
这是一个基本的演示

如何开始?

还要注意的是,我是一个PHPNoob,第一次使用这个api。。。所以我的能力暂时还没有超越这一点。其中大部分是从您正在使用的php包装库附带的演示中“借用”来的

但希望能有所帮助:)


另外,不确定是否有办法在不调用其他窗口的情况下自动化OAuth进程。

您能提供更多信息吗?你期望什么,你得到了什么?也许是一些代码示例。@Ikke嗨!谢谢你这么快回答。我需要从我的Soundcloud帐户中检索最后2首曲目。在我需要库中的文件(soundcloud.php和oauth.php)之后,我需要设置四个参数:$consumer\u key、$consumer\u secret、$callback\u url、$tmp\u path。我已经有了密钥和可写缓存文件夹。我不知道我的回调url是什么。此外,我必须说我找不到任何示例代码,所以我甚至不能开始编写任何东西。太堵了!任何建议都将不胜感激。将此添加到问题中以改进问题。$callback\u url是您网页的url。例如,我的是,因为这就是我的代码所在。我现在正在做一些类似的事情,所以我可能会在一点时间内提供更好的帮助!另外,我刚刚注意到登录/注销需要两次尝试才能成功:(