Search code examples
phpapisoundcloud

soundcloud api php add follower


I am making a page where you can log in with your soundcloud and gain access to download my songs as long as you are following me on soundcloud. I have authentication working but cannot figure out the following part. I pretty much copied and pasted the examples from the soundcloud developers website under the like and follow section, and it seams neither is working how its supposed to. The try catch makes it appear that i am not following my main account (which i got the user id# from testing the authentication step) even if i go on soundcloud, on my test account and follow my main account. Here is the error I am getting:

Warning:  Missing argument 2 for Services_Soundcloud::put(), called in /my_website/index.php on line 43 and defined in /my_website/Services/Soundcloud.php on line 636



Notice:  Undefined variable: postData in /my_website/Services/Soundcloud.php on line 642



Fatal error:  Uncaught exception 'Services_Soundcloud_Invalid_Http_Response_Code_Exception' with message 'The requested URL responded with HTTP code 404.' in /my_website/Services/Soundcloud.php:941
Stack trace:
#0 /my_website/Services/Soundcloud.php(645): Services_Soundcloud->_request('https://api.sou...', Array)
#1 /my_website/index.php(43): Services_Soundcloud->put('/me/followings/...')
#2 {main}
  thrown in my_website/Services/Soundcloud.php on line 941

heres the code im running:

require_once 'Services/Soundcloud.php';
//session_destroy();
session_start();

$soundcloud = new Services_Soundcloud(client_id, secret_id, redirect_uri)
$authURL = $soundcloud->getAuthorizeUrl();

echo "<pre>";

if (!empty ($_SESSION['token'])){
        $soundcloud->setAccessToken($_SESSION['token']);
} else if(!empty($_GET['code'])){
        try{
                $accessToken = $soundcloud->accessToken($_GET['code']);
                $_SESSION['token'] = $accessToken['access_token'];
                $soundcloud->setAccessToken($_SESSION['token']);
        } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
                exit($e->getMessage());
        }
} else {
        echo "<a href='".$authURL."'><img border='0' alt='Connect with Soundcloud' src='connect.png'></a>";
}

if (!empty ($_SESSION['token'])){
        // check the status of the relationship
        echo $_SESSION['token'];
        try {
                $soundcloud->get('/me/followings/#######');
        } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
                if ($e->getHttpCode() == '404')
                        print "You are not following user #######\nTrying to follow now\n";
                        $soundcloud->put('/me/followings/#######');
        }
}

Are the examples on soundcloud wrong or am i doing something wrong before i get to those commands?

also please note that my soundcloud object init and followings/##### was changed to protect my information.


Solution

  • after a lot of research and trial and error if figured out that setting the path to:

    https://api.soundcloud.com/me/followings/#######?oauth_token=MY_TOKEN
    

    seems to work. I don't know if this is just a workaround or how the API was intended to be used but its the only way i could get it to work, i was assuming the soundcloud client object was sending the token automatically with the command but it appears not.

    I have also figured out that if it does find a match (user is following the id number) then it returns a 303 error, which the api guide does not even list in their http error codes.