如何获取没有类型的音云的json

问题描述 投票:1回答:1

我使用【A】部分中的代码从Soundcloud API获取JSON。

但是我想不使用$type就得到它,就像代码【B】一样。

换句话说,我只想通过提供$target来获得该信息。

我该怎么办?


$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;
}

((添加2020-02-21-09:38 @Tokyo)

我尝试了此代码【C】,但也失败了。

    // 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;

((添加2020-02-21-10:12 @Tokyo)

我尝试过此代码【D】,但也失败了。

    // 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";
php soundcloud
1个回答
0
投票

我尝试过此代码【E】成功了!

感谢您给我很好的建议,@ 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";
© www.soinside.com 2019 - 2024. All rights reserved.