koyubin
koyubin

Reputation: 21

How to get json for soundcloud without type

I get JSON from the Soundcloud API by using code in section【A】.

But I want to get it without using $type, like in code【B】.

In other words, I want to get that information by only giving $target.

What should I do?


$r = soundcloud_responce();
var_dump( $r );

function soundcloud_responce(){

    $client_id = 'xxx'; 
    $type = 'tracks';
    $q = 'words';

    // code【A】
    // If I have $type, So this process ok.
    $url = "https://api.soundcloud.com/";
    $url .= $type;
    $url .= "?client_id=$client_id";    
    $url .= "&q=$q";

    // code【B】
    // I want to do same process with $target but without $type
    $target = "https://soundcloud.com/accountname/trackname";
    $target = str_replace('https://soundcloud.com/', '', $target);
    $url = "https://api.soundcloud.com/";
    $url .= $target;
    $url .= "?client_id=$client_id";        

    // curl
    $ch = curl_init();
    $headers = [
        'Accept: application/json',
        'Content-Type: application/json',
    ];
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $res = curl_exec($ch);
    $json = json_decode($res);
    curl_close($ch);
    return $json;
}

(Add 2020-02-21-09:38 @Tokyo)

I tried this code【C】but this also failed.

    // code【C】
    // I tried with oembed but this also failed.
    $target = "https://soundcloud.com/accountname/trackname";
    $url = 'http://soundcloud.com/oembed?format=json&url='.$target;

(Add 2020-02-21-10:12 @Tokyo)

I tried this code【D】but this also failed.

    // code【D】
    // I tried with resolve but this also failed.
    $target = "https://soundcloud.com/accountname/trackname";
    $url = "https://api.soundcloud.com/resolve?url=$target&client_id=$client_id";

Upvotes: 1

Views: 444

Answers (1)

koyubin
koyubin

Reputation: 21

I tried this code【E】this is successful!

Thank you for giving me good advice, @showdev.

    // code【E】
    // this is successful!
    $target = "https://soundcloud.com/accountname/trackname";
    $target = urlencode($target);
    $url = "https://api.soundcloud.com/resolve.json?url=$target&client_id=$client_id";

Upvotes: 1

Related Questions