这是一个很有趣的问题。
我正在为一个微型网络无线电播放器编写一个API。为了获得当前的跟踪,我将解析cast服务器的前端。
在http://deepmix.ru的示例中,它是一个SHOUTcast 1.x服务器,我可以解析URI http://85.21.79.31:7128/played.html。
当我在FireFox中请求URI时,我得到了显示播放曲目列表的网页。如果我从承载API的服务器BASH请求带有cURL的URI,就会得到该服务器的404。
$ curl -v -G http://85.21.79.31:7128/played.html
* Trying 85.21.79.31...
* Connected to 85.21.79.31 (85.21.79.31) port 7128 (#0)
> GET /played.html HTTP/1.1
> Host: 85.21.79.31:7128
> User-Agent: curl/7.47.0
> Accept: */*
>
ICY 404 Resource Not Found
icy-notice1:<BR>SHOUTcast Distributed Network Audio Server/Linux v1.9.8<BR>
icy-notice2:The resource requested was not found<BR>
* Connection #0 to host 85.21.79.31 left intact我假设一个合适的用户代理可能会帮助并添加Mozilla来接收网页。所以起作用了。
$ curl -v -A "Mozilla" -G http://85.21.79.31:7128/played.html
* Trying 85.21.79.31...
* Connected to 85.21.79.31 (85.21.79.31) port 7128 (#0)
> GET /played.html HTTP/1.1
> Host: 85.21.79.31:7128
> User-Agent: Mozilla
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< content-type:text/html
<
<HTML>[...]<title>SHOUTcast Administrator</title>[...]</HEAD><BODY topmargin=0 leftmargin=0 marginheight=0 marginwidth=0 bgcolor=#000000 text=#EEEEEE link=#001155 vlink=#001155 alink=#FF0000><font class=default><table width=100% border=0 cellpadding=0 cellspacing=0><tr><td height=50><font class=logoText> SHOUTcast Song History</font></td></tr><tr><td height=14 align=right><font class=ltv><a id=ltv href="http://www.shoutcast.com/">SHOUTcast Server Version 1.9.8/Linux</a>[...]</body></html>根据我的发现,我将请求转移到我的PHP实现中。
$curlHandler = curl_init();
curl_setopt_array(
$curlHandler,
[
CURLINFO_HEADER_OUT => true,
CURLOPT_URL => 'http://85.21.79.31:7128/played.html',
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_USERAGENT => 'Mozilla',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_NOBODY => true
]
);
$response = curl_exec( $curlHandler );
$info = curl_getinfo( $curlHandler );
curl_close( $curlHandler );
var_dump( $info );
var_dump( $response );但我得到了404的回复。
array(31) {
["request_header"]=>
string(87) "GET /played.html HTTP/1.1
Host: 85.21.79.31:7128
User-Agent: Mozilla
Accept: */*
"
}Warning: file_get_contents(http://85.21.79.31:7128/played.html): failed to open stream: HTTP request failed! ICY 404 Resource Not Found
in /vagrant/src/Readers/CurrentTrackReader.php on line 50在比较请求头时,没有区别。因此,我假设BASH和PHP的cURL实现之间存在差异,我看不出来。
那么这里发生了什么?
更新(2019-07-31)
附加运行时环境信息
操作系统
Linux hostname 4.19.0-0.bpo.5-amd64 #1 SMP Debian 4.19.37-4~bpo9+1 (2019-06-19) x86_64 GNU/LinuxPHP
PHP Version 7.3.7-2+0~20190725.42+debian9~1.gbp848ca5更新(2019-07-31)
关于cURL的其他信息
巴什
$ curl --version
curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP UnixSockets phpinfo()
cURL support | enabled
cURL Information | 7.52.1
Age | 3
Features
AsynchDNS | Yes
CharConv | No
Debug | No
GSS-Negotiate | No
IDN | Yes
IPv6 | Yes
krb4 | No
Largefile | Yes
libz | Yes
NTLM | Yes
NTLMWB | Yes
SPNEGO | Yes
SSL | Yes
SSPI | No
TLS-SRP | Yes
HTTP2 | Yes
GSSAPI | Yes
KERBEROS5 | Yes
UNIX_SOCKETS | Yes
PSL | Yes
HTTPS_PROXY | Yes
Protocols | dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, ldaps, pop3, pop3s, rtmp, rtsp, scp, sftp, smb, smbs, smtp, smtps, telnet, tftp
Host | x86_64-pc-linux-gnu
SSL Version | OpenSSL/1.0.2s
ZLib Version | 1.2.8
libSSH Version | libssh2/1.7.0
Directive | Local Value | Master Value
curl.cainfo | no value | no value发布于 2019-08-01 08:13:10
这是我的一个很大的错误。
首先,我将CURLOPT_NOBODY => true留在代码中(请参阅问题),这将导致一个空的响应。
其次,我在代码中留下了file_get_contents( $uri ),这导致了问题中发布的警告。
总之,在删除这两个cURL时,它们都可以正常工作。
https://stackoverflow.com/questions/57298137
复制相似问题