Right way to Login With SoundCloud using API in PHP, create Login With SoundCloud easily, tutorial & example PHP script for download.

How To Create Login With SoundCloud In PHP

Very easy, using SoundCloud API and Qassim_HTTP() Function, we will create login with SoundCloud.

Create SoundCloud App

Go to SoundCloud App and register a new App, we need “client_id” and “client_secret” and “redirect_uri”.

login with soundcloud api php

Create Login With SoundCloud

Now using SoundCloud App, we will create login with SoundCloud, we need 6 PHP files:

  1. index.php
  2. config.php
  3. login.php
  4. callback.php
  5. Qassim_HTTP.php
  6. logout.php

Create new PHP file “index.php” and enter this code inside it:

<?php

    // Script By Qassim Hassan, wp-time.com

    session_start();

    if( isset($_SESSION['user_info']) ){ // if user is logged in
        $user_info = $_SESSION['user_info'];
        ?>
            <h3>Welcome <?php echo $user_info["first_name"]; ?> <?php echo $user_info["last_name"]; ?> !</h3>
            <p><img src="<?php echo str_replace("large", "t300x300", $user_info['avatar_url']); // you can change image size, for example t600x600 ?>"></p>
            <p>Username: <?php echo $user_info["username"]; ?></p>
            <p>Followers: <?php echo $user_info["followers_count"]; ?></p>
            <p>Followings: <?php echo $user_info["followings_count"]; ?></p>
            <p><a href="logout.php">Logout</a></p>
        <?php
    }

    else{ // if user is not logged in
        echo '<a href="login.php">Login With SoundCloud</a>';
    }

?>

Now create new PHP file “config.php” and enter this code inside it:

<?php

    // Script By Qassim Hassan, wp-time.com

    // go to http://soundcloud.com/you/apps/new and register a new app

    $client_id = "xxxxxx"; // enter your client id

    $client_secret = "xxxxxx"; // enter your client secret

    $redirect_uri = "http://localhost/login-with-soundcloud/callback.php"; // enter your redirect url

?>

Now create new PHP file “login.php” and enter this code inside it:

<?php

    session_start();

    // Script By Qassim Hassan, wp-time.com

    if( isset($_SESSION['user_info']) ){ // if user is logged in
        header("location: index.php"); // redirect user to index page
        return false;
    }

    include 'config.php'; // include app data

    $_SESSION['login'] = 1;

    header("location: https://soundcloud.com/connect?client_id=$client_id&response_type=code&redirect_uri=$redirect_uri"); // redirect user to oauth page

?>

Now create new PHP file “callback.php” and enter this code inside it:

<?php

    // Script By Qassim Hassan, wp-time.com

    session_start();

    if( isset($_SESSION['user_info']) or !isset($_SESSION['login']) ){ // if user is logged in
        header("location: index.php"); // redirect user to index page
        return false;
    }

    include 'Qassim_HTTP.php'; // include Qassim_HTTP() function

    include 'config.php'; // include app data

    $code = $_GET['code'];

    /* Get User Access Token */

    $method = 1; // method = 1, because we want POST method

    $url = "https://api.soundcloud.com/oauth2/token";

    $header = 0; // header = 0, because we do not have header

    $data = array(
        "client_id" => $client_id,
        "client_secret" => $client_secret,
        "redirect_uri" => $redirect_uri,
        "grant_type" => "authorization_code",
        "code" => $code
    );

    $json = 1; // json = 1, because we want JSON response

    $get_access_token = Qassim_HTTP($method, $url, $header, $data, $json);

    $access_token = $get_access_token['access_token']; // user access token

    /* Get User Info */

    $get_user_info = file_get_contents("https://api.soundcloud.com/me?oauth_token=$access_token");

    $result = json_decode($get_user_info, true);

    $_SESSION['user_info'] = $result; // save user info in session

    header("location: index.php"); // redirect user to index page

?>

Now create new PHP file “Qassim_HTTP.php” and enter this code inside it:


<?php

/* Function By Qassim Hassan, wp-time.com */
function Qassim_HTTP($method, $url, $header, $data, $json){
if( $method == 1 ){
$method_type = 1; // 1 = POST
}else{
$method_type = 0; // 0 = GET
}

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HEADER, 0);

if( $header !== 0 ){
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
}

curl_setopt($curl, CURLOPT_POST, $method_type);

if( $data !== 0 ){
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}

$response = curl_exec($curl);

if( $json == 0 ){
$json = $response;
}else{
$json = json_decode($response, true);
}

curl_close($curl);

return $json;
}

?>

Finally,¬†create new PHP file “logout.php” and enter this code inside it:

<?php

    session_start();

    /* Script By Qassim Hassan, wp-time.com */

    // if user is logged in, destroy all facebook sessions
    if( isset($_SESSION['user_info']) or isset($_SESSION['login']) ){
        unset( $_SESSION['user_info'] ); // destroy
        unset( $_SESSION['login'] ); // destroy
        header("location: index.php"); // redirect user to index page
    }

    else{ // if user is not logged in
        header("location: index.php"); // redirect user to index page
    }

?>

Done.

Login With SoundCloud Demo

Check live demo.

Download

Download login with SoundCloud PHP Script and Live Demo example.

More

Check more Tutorial of Login with API.