Soundcloud API, PHP and OAuth

I am creating a website and I need to request my last two tracks from my soundcloud account and display them on my page. I read the Soundcloud API documentation, but it seems obscure and far from my reach. I installed the PHP library to use the API and Oauth and set up the SoundCloud application to get my consumer keys, but I cannot start an OAuth session.

I am using this library .

I need to get the last 2 tracks from my Soundcloud account. After I need 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 keys and a folder for writing in the cache. I don't know what my callback url is. Also, I have to say that I can’t find the working example code, so I can’t even write anything. So blocked!

Is there a way to automate the OAuth process without invoking another window, so my OAuth token is being requested in my PHP script ?.

I was wondering, maybe you could pass me some sample code for this. It would be great!

+3
source share
1 answer

This may be useful for you. After logging in, he inserts my favorite tracks onto the page. You can change $ favs to load your own songs instead of your favorites.

, config.php user_key, consumer_secret callback_url.

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

, , php script.

<?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 noob, api ... . "" , php-, .

, :)

. , OAuth, .

+2

Source: https://habr.com/ru/post/1742859/


All Articles