【问题标题】:Rendering SoundCloud widget for a private track using PHP API使用 PHP API 为私有轨道渲染 SoundCloud 小部件
【发布时间】:2012-09-28 13:52:53
【问题描述】:

我正在尝试使用 PHP API 呈现 SoundCloud HTML5 小部件,但每次运行我认为应该返回小部件的 HTML 的命令时,我都会得到一个异常:

The requested URL responded with HTTP code 302

我意识到这是一个重定向。我不知道为什么这就是我所得到的全部,或者如何处理它才能真正获得小部件 HTML。

API 上的文档说要使用 PHP 嵌入小部件,您应该这样做:

<?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'];

我正在运行这个:

// 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());
    }

其中“track_url”实际上是我使用相同 API 在应用程序早期向 SoundCloud 询问轨道对象时返回的 URL。

我实际上并不确定这个 URL 是否正确,因为我返回的跟踪对象在表单中给出了“uri”:

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

文档示例都有一个直接的http://soundcloud.com/username/track-permalink URL - 但即使使用公共轨道的已知路径,尝试运行 API oembed 方法也会失败...我仍然得到 302 异常。

最后,在“get”命令中提到了将“allow_redirects”设置为 false,但是当我添加到用于构建 API 查询的参数时,这没有效果。我也尝试添加额外的 cURL 选项,但这也没有效果。

我确实启用了对 SoundCloud 中曲目的 API 访问。

这有点把我的头撞到墙上了。如果有人有任何指示,我将非常感激听到他们的声音。只是为了清楚起见,我可以通过我创建的 API 实例访问所有用户数据、cmets 等,因此它似乎工作正常。

【问题讨论】:

  • HTTP code 302 直截了当...这意味着您已被重定向.....CURLOPT_FOLLOWLOCATION 仅在您使用download 方法时才会激活
  • 我知道 302 是重定向。我不明白为什么会这样。感谢您对 CURLOPT_FOLLOWLOCATION 的澄清。
  • Paul Osman 下面的修复可以解决问题 - 非常好。只是一个简短的说明(我第一次错过了)-尝试使用私人曲目 URL 时,请确保使用曲目页面右侧面板中显示的“秘密链接”URL。这将是曲目的漂亮 URL,末尾带有随机字符串。

标签: php api soundcloud http-status-code-302


【解决方案1】:

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

<?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,如上面的 cmets 中所述。
  • 您需要将来自$client-&gt;get 的返回包裹在json_decode
  • 结果是stdClass 对象,而不是Array,因此必须使用-&gt; 运算符访问html 属性。

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

【讨论】:

  • 太棒了——就是这样!非常感谢保罗。
  • 我在 Ruby gem 中的私有轨道上遇到了类似的问题。当我在私人轨道上运行 $sc_client.get('/oembed', :url => track.permalink_url) 时,我得到一个 404——公共轨道工作正常——私人轨道是“为所有人启用小部件”和“启用应用程序” "
  • 从头开始,你必须使用#secret_uri
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多