Giter Club home page Giter Club logo

php-soundcloud's Introduction

SoundCloud PHP API Wrapper

Introduction

A wrapper for the SoundCloud API written in PHP with support for authentication using OAuth 2.0.

The wrapper got a real overhaul with version 2.0. The current version was written with PEAR in mind and can easily by distributed as a PEAR package.

Requirements

  • PHP >= 5.0.0 (with cURL support)

Getting started

Check out the getting started wiki entry for further reference on how to get started. Also make sure to check out the demo application for some example code.

Composer

A version of the library that is PSR-4 compliant and installable via Composer is available on the psr-4 branch. To include it in your projects, include the following in your composer.json file:

{
    "repositories": [
        {
            "type": "git",
            "url": "https://github.com/mptre/php-soundcloud"
        }
    ],
    "require": {
        "glenscott/soundcloud": "dev-psr-4"
    }
}

The Composer-version of the libray requires PHP >= 5.3 because of the use of namespaces.

The namespaced class names differ from the examples below. For example, to create a new client object with app credentials, instead of:

$client = new Services_Soundcloud(
  'CLIENT_ID', 'CLIENT_SECRET', 'REDIRECT_URL');

Instead, use:

$client = new SoundCloud\Client(
    'CLIENT_ID',
    'CLIENT_SECRET',
    'REDIRECT_URL'
);

Examples

The wrapper includes convenient methods used to perform HTTP requests on behalf of the authenticated user. Below you'll find a few quick examples.

Ofcourse you need to handle the authentication first before being able to request and modify protect resources as demonstrated below. Therefor I refer to the demo application which got some example code on how to handle authentication.

GET

<?php
try {
    $response = json_decode($soundcloud->get('me'), true);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
    exit($e->getMessage());
}

POST

<?php
$comment = <<<EOH
<comment>
    <body>Yeah!</body>
</comment>
EOH;

try {
    $response = json_decode(
        $soundcloud->post(
            'tracks/1/comments',
            $comment,
            array(CURLOPT_HTTPHEADER => array('Content-Type: application/xml'))
        ),
        true
    );
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
    exit($e->getMessage());
}

PUT

<?php
$track = <<<EOH
<track>
    <downloadable>true</downloadable>
</track>
EOH;

try {
    $response = json_decode(
        $soundcloud->put(
            'tracks/1',
            $track,
            array(CURLOPT_HTTPHEADER => array('Content-Type: application/xml'))
        ),
        true
    );
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
    exit($e->getMessage());
}

DELETE

<?php
try {
    $response = json_decode($soundcloud->delete('tracks/1'), true);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
    exit($e->getMessage());
}

Upload track

<?php
$track = array(
    'track[title]' => 'My awesome track',
    'track[tags]' => 'dubstep rofl',
    'track[asset_data]' => '@/absolute/path/to/track.mp3'
);
    
try {
    $response = $soundcloud->post('tracks', $track);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
    exit($e->getMessage());
}

Download track

<?php
try {
    $track = $soundcloud->download(1337);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
    exit($e->getMessage());
}

// do something clever with $track. Save to file perhaps?

Update playlist

Many found it difficult to update a existing playlist. Therefor a custom method is available to simplify this matter.

<?php
$playlistId = 2000;
$trackIds = array(2001);
$optionalFields = array('title' => 'My awesome playlist');

try {
    $playlist = $soundcloud->updatePlaylist($playlistId, $trackIds, $optionalFields);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
    exit($e->getMessage());
}

Feedback and questions

Found a bug or missing a feature? Don't hesitate to create a new issue here on GitHub.

Also make sure to check out the official documentation and the join Google Group in order to stay updated.

License

Copyright (c) 2011 Anton Lindqvist

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

php-soundcloud's People

Contributors

ben-xo avatar glenscott avatar hmanprod avatar lynicidn avatar nonaltrx avatar por avatar srhaber avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.