基于PHP的Soundcloud解析

基于PHP的Soundcloud解析,php,soundcloud,Php,Soundcloud,我正在尝试设置一个简单的应用程序,它应该: 从艺术家的URL获取其客户端ID 显示两个最新曲目 然而,我对soundcloud不是那么实用,我只知道基本的php。我开始玩soundcloud,但我无法处理它。我的一个问题是,我写的任何代码都会 致命错误:未捕获异常“服务\u声音云\u无效\u Http\u响应\u代码\u异常”,消息为“请求的URL以Http代码302响应”。 最简单的设置直接来自文档,是从给定URL开始从track id检索注释的示例 <?php require_on

我正在尝试设置一个简单的应用程序,它应该:

  • 从艺术家的URL获取其客户端ID
  • 显示两个最新曲目
然而,我对soundcloud不是那么实用,我只知道基本的php。我开始玩soundcloud,但我无法处理它。我的一个问题是,我写的任何代码都会

致命错误:未捕获异常“服务\u声音云\u无效\u Http\u响应\u代码\u异常”,消息为“请求的URL以Http代码302响应”。

最简单的设置直接来自文档,是从给定URL开始从track id检索注释的示例

<?php
require_once 'Services/Soundcloud.php';

// create a client object with your app credentials
$client = new Services_Soundcloud('my_client','my_secret');

// a permalink to a track
$track_url = 'https://url_to_a_track';

// resolve track URL into track resource
$track = json_decode($client->get('resolve', array('url' => $track_url), array('CURLOPT_FOLLOWLOCATION', TRUE )));

// now that we have the track id, we can get a list of comments, for example
foreach (json_decode($client->get('tracks/' . $track->id . 'comments')) as $c)
print 'Someone said: ' . $c->body . ' at ' . $c->timestamp . "\n"; ?>


刚刚添加了(
'CURLOPT_FOLLOWLOCATION',TRUE
),因为我已经在网上读到了它。。。我总是犯致命的错误。。。为什么?我已经解决了。如果资源是公共的,则不需要进行身份验证。以下是代码:

  • 从wordpress自定义字段获取用户URL
  • 从json对象检索ID
  • 检索最新的2首曲目并显示2个嵌入式播放器
它只需要您自己的客户端ID,您可以通过在SoundCloudDevelopers部分注册轻松获得,然后将其替换为{your_ID}

    // get user URL from Wordpress custom field
    $sc_url = get_post_meta(get_the_id(), 'sc_url', true);

    // if $sc_url is not empty, do
    if (!empty($sc_url)) {

    $unparsed_json = file_get_contents('https://api.soundcloud.com/resolve.json?url='.$sc_url.'&client_id={Your_ID}');

    $json_object = json_decode($unparsed_json);

    // retrieve the user ID from json_object
    $roster_id = $json_object->{'id'};

    // get last two tracks from the user and generate embed code for each tracks 
    $tracks_json = file_get_contents('http://api.soundcloud.com/users/'.$roster_id.'/tracks?client_id={Your_ID}&order=latest&limit=2&format=json');
    $tracks = json_decode($tracks_json);
    foreach ($tracks as $track){
    $trackID = $track->id;
    echo '<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F'.$trackID.'"></iframe>';
    }}
//从Wordpress自定义字段获取用户URL
$sc_url=get_post_meta(get_id(),'sc_url',true);
//如果$sc_url不为空,请执行以下操作:
如果(!空($sc_url)){
$unparsed\u json=file\u get\u contents('https://api.soundcloud.com/resolve.json?url=“.$sc_url.&client_id={Your_id}”);
$json\u object=json\u decode($unparsed\u json);
//从json_对象检索用户ID
$LOSTERS\U id=$json\U对象->{'id'};
//从用户处获取最后两首曲目,并为每首曲目生成嵌入代码
$tracks\u json=file\u get\u contents('http://api.soundcloud.com/users/“.$LOSTUST\u id.”/tracks?client\u id={Your\u id}&order=latest&limit=2&format=json');
$tracks=json\u decode($tracks\u json);
foreach($tracks作为$track){
$trackID=$track->id;
回声';
}}

希望它能帮助其他人:)

我如何在PHP中使用SoundCloudAPI解析资源

  • 要在php中使用soundcloud API的resolve资源,请遵循frafor的代码,执行以下操作:

    $client->setCurlOptions(数组(CURLOPT_FOLLOWLOCATION=>1));
    $response=json_decode($client->get('resolve',array('url'=>$track_url));
    
    ?>
因此,首先是CURLOPT_FOLLOWLOCATION,然后是API调用,您的where几乎在那里!:) 希望它能帮助别人

干杯,
T

soundcloud服务器已经切换到安全模式,现在它们也使用https协议来实现API/JSON

我的应用程序不再工作了,所以这些是获取JSON的其他cURL选项。 SSL验证必须同时设置为禁用。我只能用这个

// Configuring curl options
$options = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => array('Content-type: application/json'),
    CURLOPT_SSL_VERIFYHOST => 0,
    CURLOPT_SSL_VERIFYPEER => 0,
);
希望能有帮助