[英]Different responses of BASH cURL and PHP cURL with identical request headers
這是一個非常有趣的問題。
我正在為微型網絡廣播播放器編寫 API。 為了獲得當前曲目,我正在解析演員服務器前端。
在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 cURL 實現中。
$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/Linux
PHP
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
這是我自己的一個很大的錯誤。
首先,我在代碼中留下了CURLOPT_NOBODY => true
(請參閱問題),這會導致空響應。
其次,我在我的代碼中留下了file_get_contents( $uri )
,這導致了問題中發布的警告。
總之,在刪除這些時,兩個 cURL 都按預期工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.