Soundcloud API、PHP 和 OAuth

发布于 2024-08-29 23:30:37 字数 639 浏览 10 评论 0原文

我正在构建一个网站,我需要从我的 soundcloud 帐户查询我的最后两个曲目并将它们显示在我的页面上。 我已经阅读了 Soundcloud API 文档,但它似乎晦涩难懂并且远远超出了我的能力范围。 我已经安装了用于使用 API 和 Oauth 的 PHP 库,并设置了 SoundCloud 应用程序来获取消费者密钥,但我无法启动 OAuth 会话。

我正在使用这个

我需要从我的 Soundcloud 帐户中检索最后 2 首曲目。在我需要库中的文件(soundcloud.php 和 oauth.php)后,我需要设置四个参数:$consumer_key、$consumer_secret、$callback_url、$tmp_path。

我已经有了我的密钥和可写缓存文件夹。我不知道我的回调网址是什么。另外,我必须说我找不到任何工作示例代码,所以我什至无法开始编写任何内容。就这样被堵住了!

有没有办法在不调用另一个窗口的情况下自动化 OAuth 过程,以便在我的 PHP 脚本中请求我的 OAuth 令牌?

我想知道您是否可以给我一些示例代码来执行此操作。 那太好了!

I'm building a site, and I need to query my last two tracks from my soundcloud account and display them on my page.
I've read the Soundcloud API documentation but it seems obscure and far from my reach.
I've installed the PHP library for using the API and Oauth, and set up my SoundCloud application to get my Consumer Keys, but I can't start the OAuth session.

I'm using this library.

I need to retrieve the last 2 tracks from my Soundcloud account. After I require the files from the library (soundcloud.php and oauth.php) I need to set four parameters: $consumer_key, $consumer_secret, $callback_url, $tmp_path.

I already have my keys and my writable cache folder. I don't know what's my callback url. In addition, I must say I can't find any working example code, so I can not even start to write anything. So blocked!

Is there any way to automatize the OAuth process without invoking another window, so my OAuth Token is requested in my PHP script?.

I was wondering if maybe you could hand me some sample code for doing this.
That would be great!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你又不是我 2024-09-05 23:30:37

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

另请注意,我的 config.php 包括我的consumer_key、c​​onsumer_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 过程。

This may be useful to you. After logging in, it embeds my favourite tracks into the page.
You could change $favs to loads your own songs instead of your favourites instead.

Also note, my config.php includes my consumer_key, consumer_secret and my callback_url.

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

You want it to equal the place where your php script is.

<?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>

Also note, that I'm a php noob, using this api for the first time... so my abilities don't stretch past this for the moment. Most of this was "borrowed" from the demo that comes with the php wrapper library you're using.

Hope it helps though :)

ps. Not sure if there is a way to automatize the OAuth process without invoking another window yet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文