通过 PHP CURL 检索 Soundcloud 媒体 URL?
我使用 Soundcloud API + PHP 来提取有关 soundcloud 曲目的信息,以便将 jPlayer 用作播放列表的 mp3 播放器。现在,我可以完全访问有关该曲目的所有信息(除了 mp3 的实际链接之外),以便播放该曲目。
根据 API,如果曲目允许流式传输,您只需 cURL 流 URL(即: http://api.soundcloud.com/tracks/24887826/stream?client_id=xxx)。
现在,如果我尝试 cURL,我的结果是一个简单的 HTML 位 您正在 重定向。
。
我想要的链接是整个 media.soundcloud.com/sjkn57c6gAA5 ,仅此而已。但是,如果我尝试对 '//a/@href'
进行 xpath,则不会返回任何结果。谁能指出如何获取此链接的正确方向,以便我可以生成该文件的适当链接?
提前致谢!
特雷
I'm using the Soundcloud API + PHP to pull information about soundcloud tracks in order to use jPlayer as a mp3 player for playlists. Now, I am able to completely access all the information about the track except the actual link to the mp3, in order to play the track.
According to the API, if the track allows streaming, you just have to cURL the stream URL (ie: http://api.soundcloud.com/tracks/24887826/stream?client_id=xxx).
Now, If I try to cURL that, my results are a simple HTML bit <html><body>You are being <a href="http://ak-media.soundcloud.com/sjkn57c6gAA5.128.mp3?AWSAccessKeyId=AKIAJBHW5FB4ERKUQUOQ&Expires=1318411541&Signature=yaX7Noe%2F8c5dFF0H%2BGfhZ%2FX0130%3D&__gda__=1318411541_6ed9d2af39e51b5f1e94e659eff0495d">redirected</a>.</body></html>
.
The link I want is the whole media.soundcloud.com/sjkn57c6gAA5
and that's it. However, if I try to xpath the '//a/@href'
I get no results returned. Can anyone point me in to the right direction of how to grab this link so I can generate the appropriate link to the file?
Thanks ahead of time!
Tre
如果您在 HTML 上使用 xpath(顺便说一下,不推荐,因为 HTML 不是 XML 的子集,除非它是 XHTML 并且不需要关闭某些标签),那么它不应该是 //html/body/a /@href?
否则,您可以使用正则表达式来提取 URL:
或者您可以缩小范围以剥离其他部分,但您需要非常小心,因为我怀疑 API 能否保证 URL 始终采用相同的格式(例如子域将始终以 media 结尾)。
If you're using xpath on HTML (not recommended by the way, as HTML is not a subset of XML unless it's XHTML and doesn't require some tags to be closed), shouldn't it be //html/body/a/@href?
Otherwise, you could use a regular expression to extract the URL:
Or you could narrow it down to strip the other parts but you need to be very careful, because I doubt the API guarantees the URL will always be of the same format (e.g. the subdomain will always end with media).