mptre / php-soundcloud

API Wrapper for SoundCloud written in PHP with support for authication using OAuth.

Home | Edit | New

Getting started

This is a basic example of how to implement the SoundCloud API wrapper in your PHP application. Remember this is just a quick one to get you started. All kind of error handling and validation of user generated data has been stripped out for sake of simplicity.

1. Register your application

Head over to SoundCloud and register your new OAuth application in order to maintain your consumer key and secret. These two strings will identify your application. The keys could either be stored as constants, variables or using a database. Whatever suits you best. Also do remember to store your callback url since we’ll need it further on.

2. Download the PHP wrapper

Simply download the wrapper using git (see example below). If you’re not familiar with git you could download the whole library directly from GitHub and place the files in your desired location.


$ git clone git://github.com/mptre/php-soundcloud.git

3. Create a new Soundcloud object

Make sure both the generic OAuth and Soundcloud class are included in your application. Then initialize the Soundcloud class. The constructor takes for parameters. At this step you’ll only need or consumer key and secret.


$soundcloud = new Soundcloud('consumer-key', 'consumer-secret');

4. Authentication Step 1: Get a Request Token from SoundCloud

This is the first step of the authentication. Send a request to SoundCloud asking for request tokens. Make sure to include your callback url (added in OAuth 1.0a). The function namned get_request_token will return an associative array including request token and secret.


$token = $soundcloud->get_request_token('callback-url');

5. Authentication Step 2: Authorize the Request Token

Ask the user to grant permissions to his/her SoundCloud account. This allows your application access the given account. Note that we use the request token from previous step.


echo $soundcloud->get_authorize_url($token['oauth_token']);

6. Authentication Step 3: Authorize the Request Token

If all went well the user will be redirected to your predefined callback url. Note that a parameter namned oauth_verifier will be included in the query string. You’ll need this one when requesting access tokens.

Overwrite the Soundcloud object with your new request tokens. Then ask for access tokens using the get_access_token function where you’ll need to include the oauth_verifier.


<?php
$soundcloud = new Soundcloud('consumer-key', 'consumer-secret', $token['oauth_token'], $token['oauth_token_secret']);
$token = $soundcloud->get_access_token($_GET['oauth_verifier']);
?>

Now with your access tokens you can create a fully authorized Soundcloud object. This is where the fun stuff begin.


<?php
$soundcloud = new Soundcloud('consumer-key', 'consumer-secret', $token['oauth_token'], $token['oauth_token_secret']);
?>

7. Retrieve user account details

With your fully authorized Soundcloud object you can start sending real requests to Soundcloud. In the example below you’ll ask for the authenticated user’s account details.


$me = $soundcloud->request('me');

For further reference on how to get, delete, put and post data see the code for demo included in this repository.

Last edited by voxpelli, Fri Sep 18 10:11:37 -0700 2009
Home | Edit | New
Versions: