Skip to content

Instantly share code, notes, and snippets.

@PeeHaa
Created August 8, 2012 22:33
Show Gist options
  • Save PeeHaa/3299452 to your computer and use it in GitHub Desktop.
Save PeeHaa/3299452 to your computer and use it in GitHub Desktop.
soundcloud api troubles
<?php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}

if( !empty( $_GET['code'] ) ) {
    $bodyParams = [
        'code' => $code,
        'client_id' => 'xxx',
        'client_secret' => 'xxx',
        'redirect_uri' => urlencode(curPageURL()) .'%2Fsoundcloud.php',
        'grant_type' => 'authorization_code',
    ];
    
    $request = new StdRequest(
        'https://api.soundcloud.com/oauth2/token',
        'POST',
        ['Content-type' => 'application/x-www-form-urlencoded'],
        http_build_query($params)
    );

    $client = new Client();

    $response = $client->request($request);
    if( $response->getStatusCode() >= 400 ) {
        print('<pre>');var_dump('Eeeeek error: ' . $resonse->getBody());print('</pre>');
    }

    echo $response->getBody();
} elseif( !empty($_GET['go'] ) && $_GET['go'] == 'go' ) {
    $url = 'https://soundcloud.com/connect?type=web_server&client_id=xxx&redirect_uri=' . urlencode(curPageURL()) .'&response_type=code&scope=';
    header('Location: ' . $url);
} else {
    $url = curPageURL() . '?go=go';
    echo "<a href='$url'>Login with SoundCloud!</a>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment