使用普通 PHP 获取 Soundcloud 艺术作品

Grab Soundcloud artwork with plain PHP

我想在不使用 API 的情况下获取与带有普通 PHP 的 Soundcloud 音轨关联的艺术作品的 URL。 HTML 页面有一个 og:image 元标记 属性,非常适合我的需要。

例如,音轨 https://soundcloud.com/dengue/sets/nuevos-sonidos 的元 属性 看起来像这样:

<meta property="og:image" content="https://i1.sndcdn.com/artworks-000077991135-u5nvu1-t500x500.jpg">

问题是 HTTP 请求 returns 一个 301 Moved Permanently 代码,因此使用 DOMDocument class loadHTMLFile 函数会出错。

如果您真的不想 use their API(这似乎是个糟糕的决定,因为您不需要进行任何身份验证;它是完全开放的),您可以做一些简单的修改。

我没有从 cURL 获得任何重定向

~ $ curl -v https://soundcloud.com/dengue/sets/nuevos-sonidos
*   Trying 68.232.44.127...
* Connected to soundcloud.com (68.232.44.127) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
* Server certificate: *.soundcloud.com
* Server certificate: GlobalSign Domain Validation CA - SHA256 - G2
* Server certificate: GlobalSign Root CA
> GET /dengue/sets/nuevos-sonidos HTTP/1.1
> Host: soundcloud.com
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Cache-Control: private, max-age=0
< Content-Type: text/html
< Date: Sat, 07 May 2016 03:42:20 GMT
< Server: am/2
< Set-Cookie: sc_anonymous_id=363279-961735-991413-425081; path=/; expires=Tue, 05 May 2026 03:42:20 GMT; domain=.soundcloud.com
< Via: sssr
< X-Frame-Options: SAMEORIGIN
< Content-Length: 47003
<

但是如果你是,你只需要在从 PHP:

制作 cURL 之前添加这个选项
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

如果您认真从事黑客行业,为什么不这样做:

<?php

$url = `curl -L https://soundcloud.com/dengue/sets/nuevos-sonidos 2>/dev/null | grep 'og:image' | sed 's/.*og:image" content="\([^"]*\).*/\1/'`;

echo $url;

这是做什么的

~/Code/stack-overflow $ php hack.php
https://i1.sndcdn.com/artworks-000077991135-u5nvu1-t500x500.jpg