【发布时间】: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