简体   繁体   中英

Different responses of BASH cURL and PHP cURL with identical request headers

This is a kind of very interesting problem.

I'm writing an API for a micro web radio player. To get the current track I'm parsing the cast servers frontend.

In the example of http://deepmix.ru , which is a SHOUTcast 1.x server, I'd able to parse the URI http://85.21.79.31:7128/played.html .

When I request the URI in FireFox I get the webpage displaying the list of played tracks. If I request that URI with cURL from BASH of my server hosting my API I get a 404 of that server.

$ 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

I assumed a proper user agent may help and add Mozilla to receive the webpage. So that worked.

$ 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>&nbsp;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>

According to my discoveries I transferred the request into my PHP cURL implementation.

$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 );

But I got the 404 response.

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

While comparing the request headers there's no differences. So I assume there's differences between BASH's and PHP's cURL implementations I cannot see.

So what happens here?

Update (2019-07-31)

Additonal runtime environment information

OS

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

Update (2019-07-31)

Additional information about cURL

BASH

$ 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

It's a very big fault of myself.

First I left that CURLOPT_NOBODY => true in the code (see the question) which leads to an empty response.

Second I left file_get_contents( $uri ) in my code which leads to the warning posted in the question.

In conclusion while removing these both cURL works as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM