PHP code example of njasm / soundcloud

1. Go to this page and download the library: Download njasm/soundcloud library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

njasm / soundcloud example snippets


use Njasm\Soundcloud\SoundcloudFacade;
// or soundcloud if you don't need a facade for specific tasks
use Njasm\Soundcloud\Soundcloud;

$facade = new SoundcloudFacade($clientID, $clientSecret, $callbackUri);
$url = $facade->getAuthUrl();

// or inject your specific request params
$url = $facade->getAuthUrl(
    [
        'response_type' => 'code',
        'scope' => '*',
        'state' => 'my_app_state_code'
    ]
);

$facade = new SoundcloudFacade($clientID, $clientSecret, $callbackUri);
// this is your callbackUri script that will receive the $_GET['code']
$code = $_GET['code'];
$facade->codeForToken($code); 


$facade = new SoundcloudFacade($clientID, $clientSecret);
$facade->userCredentials($username, $password); // on success, access_token is set by default for next requests.
$response = $facade->get('/me')->request();
// raw/string body response
echo $response->bodyRaw();
// as object
echo $response->bodyObject()->id;
// as array
$array = $response->bodyArray();

...
$response  = $facade->get('/tracks')->asJson()->request();
// or
$response = $facade->get('/tracks')->asXml()->request();

// argument array style
$facade->get('/resolve', ['url' => 'http://www.soundcloud.com/hybrid-species']);

// chaining-methods
$response = $facade
    ->get('/resolve')
    ->setParams(['url' => 'http://www.soundcloud.com/hybrid-species']);

// or not
$facade->get('/resolve');
$facade->setParams(['url' => 'http://www.soundcloud.com/hybrid-species']);

$soundcloud = new Soundcloud($clientID, $clientSecret);
$soundcloud->get('/resolve', ['url' => 'http://www.soundcloud.com/hybrid-species']);
// only this invocation will send the request
$response = $soundcloud->request();

...
$theBodyString = $facade->request()->bodyRaw();

// after having the access token
// build the playlist data array
$playlistData = ['playlist' => ['title' => 'Great Playlist!', 'sharing' => 'public']];
$response = $soundcloud->post('/playlists', $playlistData)->request();

// now add tracks, get playlist id from response
// build tracks array
$tracks = [
    'playlist' => [
        'tracks' => [
            ['id' => 29720509], // track id
            ['id' => 26057359]  // other track id
        ]
    ]
];

// put tracks into playlist
$response = $soundcloud->put('/playlists/' . $response->bodyObject()->id, $tracks)->request();

// if you want the CURL response object from last CURL request.
$response = $facade->getCurlResponse();

// this will redirect user, sending a header Location to the track.
$response = $facade->download($trackID);
// redirect user to download URL suplied by soundcloud.
header('Loacation: ' . $response->getHeader('Location'));

// CAUTION: this will get the track into an in-memory variable in your server.
$response = $facade->download($trackID, true);
// save it to a file.
file_put_contents("great_track.mp3", $response->bodyRaw());

$trackPath = '/home/njasm/great.mp3';
$trackData = [
    'title' => 'Cool track title',
    'downloadable' => true,
    'artwork_data' => new \CURLFile('artwork.jpg'),
    // .... more metadata maybe?
];

$response = $facade->upload($trackPath, $trackData);

// or old-school trackdata array declaration also work, example.
$trackData = [
    'track[title]' => 'Cool track title',
    'track[downloadable]' => true,
    'track[artwork_data]' => new \CURLFile('artwork.jpg'),
    // .... more metadata maybe?
];

$response = $facade->upload($trackPath, $trackData);