1

I am trying to render a SoundCloud HTML5 widget using the PHP API, but every time I run the command I think should return the HTML for the widget, I simply get an Exception:

The requested URL responded with HTTP code 302

I realise this is a redirect. What I don't know is why that's all I ever get, or what to do about it to actually get the widget HTML.

The documentation on the API says that to embed the widget using PHP you should do this:

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

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

    // get a tracks oembed data
    $track_url = 'http://soundcloud.com/forss/flickermood';
    $embed_info = $client->get('/oembed', array('url' => $track_url));

    // render the html for the player widget
    print $embed_info['html'];

I'm running this:

// NB: Fully authorised SoundCloud API instance all working prior to this line
// $this->api refers to an authorised instance of Services_Soundcloud

try {   
        $widget = array_pop(
            json_decode( $this->api->get('oembed', array('url' => $track_url)) )
        );

        print_r($widget);

    } catch (Exception $e)
    {
        print_r($e->getMessage());
    }

where "track_url" is actually the URL I get back when asking SoundCloud for a track object earlier in the app using the same API.

I'm not actually sure this URL is correct in the first place, because the track object I get back gives the 'uri' in the form:

[uri] => https://api.soundcloud.com/tracks/62556508

The documentation examples all have a straight http://soundcloud.com/username/track-permalink URL - but even using a known path to a public track the attempt to run the API oembed method fails... I still get a 302 Exception.

Finally, there are mentions of setting "allow_redirects" to false in the 'get' command, but this has no effect when I add to the parameters used to build the query to the API. I also tried adding additional cURL options, but that too had no effect.

I have definitely enabled API access to the track within SoundCloud.

Kind of banging my head off the wall on this. If anyone has any pointers I'd be very grateful to hear them. Just for clarity's sake, I am able to access all the user data, comments etc. via the API instance I have created, so it appears to be working fine.

4

1 回答 1

2

感谢您指出了这一点。文档中有一个错误导致您误入歧途。对于那个很抱歉。我已经更新了文档以修复错误。这是更新的代码示例:

<?php

require_once 'Services/Soundcloud.php';

// create a client object with your app credentials
$client = new Services_Soundcloud('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET');
$client->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => 1));

// get a tracks oembed data
$track_url = 'http://soundcloud.com/forss/flickermood';
$embed_info = json_decode($client->get('oembed', array('url' => $track_url)));

// render the html for the player widget
print $embed_info->html;

注意区别:

  • 您需要将 CURLOPT_FOLLOWLOCATION 设置为 1,如上面评论中所述。
  • 您需要将退货包裹$client->getjson_decode
  • 结果是一个stdClass对象,而不是一个Array,因此html必须使用->运算符访问该属性。

希望有帮助。如果您仍然遇到问题,请随时发表评论,我会修改我的答案。

于 2012-10-10T21:15:34.640 回答