Home Blog Work

SoundCloud Tutorials:Authentication

Published on November 24, 2012

This is Sound Cloud Tutorial Series. If you are first time seeing this post go back to SoundCloud Tutorials:Getting Started and read it. In previous lesson we have discussed what is SoundCloud, how to setup app on SoundCloud and how to get the SoundCloud PHP SDK. SoundCloud Tutorials:Authentication



1)Sound Cloud Tutorials:Getting Started 2)Sound Cloud Tutorials:Authentication 3a)Sound Cloud Tutorials:Uploading Audio Files 3b)Sound Cloud Tutorials:Uploading Audio Files(AJAX) 4)Sound Cloud Tutorials:Playing Audio Files                            In this tutorial we will learn how to Authenticate user when he first time comes to your web-Application. Now Make your Folder structure as below. SoundCloud Tutorials:Authentication Folder Structure In above SoundCloud1 is our root folder. lib                 - is our SoundCloud library contains SDK files. config.php    - is The app configuration file. callback.php- is Our call back file. index.php     - in it we will have out login url. **Note:**If you are using default SoundCloud SDK files and your on windows I suggest you to download the my version from above link. Other wise you will get The requested URL responded with HTTP code 0 error. I will write about this error more clearly in other post. Now lets got to the coding part. config.php

$callback="Your-APP-CallBack Page URl";

require_once 'lib/Soundcloud.php';
$soundcloud = new Services_Soundcloud($clientId, $clientSecret, $callback);

In the previous tutorial of this series i suggested you to save the Client ID,Client Secret,Redirect URI for Authentication values in a note-pad. Just replace them in above code. Here we including Soundcloud lib. In the last line we creating $soundcloud obect so that we can use the same object for all other pages by including config.php file. index.php

$authorizeUrl = $soundcloud->getAuthorizeUrl();
<!DOCTYPE html>
<html lang="en">
<a class="login"href="<?php echo $authorizeUrl; ?>">
<img src="img/btn-connect-sc-l.png" />

Here we generating authorizeUrl using the API. callback.php

try {
$accessToken = $soundcloud->accessToken($_GET['code']);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
try {
$me = json_decode($soundcloud->get('me'), true);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {

$user_data = array(
'access_token' => $accessToken['access_token'],
'id' => $me['id'],
'username' => $me['username'],
'name' => $me['full_name'],
'avatar' => $me['avatar_url']

echo 'Welcome'.$user_data['name'];

In this code first we obtaining $accessToken. We will use it in later chapters. In second try-catch block we will get the user data. See $user_data is an array which will store assces_taken value and other user data. In this example we simply displaying a Welcome message the user.

Note:When I test this script it works perfectly in local host. When iam testing it on server I got Parse error: syntax error, unexpected T_FUNCTION, expecting ’)’ in /Soundcloud.php error. So again I modified the API code Now it working good. That’s it. In Next tutorial we will learn how Uploading Audio Files on SoundCloud.