无法将远程文件下载到服务器

Can't download a remote file to the server

远程文件是一个附件,我可以获取headers但是我无法获取文件或其中的内容。

 <?php
    error_reporting(E_ALL);

    //not working, with rb too
    //file_put_contents("natives.html", stream_get_contents(fopen("http://www.dev-c.com/nativedb/reference.html", 'r')));

    //same with rb too
    //file_put_contents("natives.html", fopen("http://www.dev-c.com/nativedb/reference.html", 'r'));

    //same
    //echo file_get_contents("http://www.dev-c.com/nativedb/reference.html");

    //same
    /*$url = 'http://www.dev-c.com/nativedb/reference.html';
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_BINARYTRANSFER, false);
    curl_setopt($curl, CURLOPT_HEADER, true);

    $data = curl_exec($curl);

    curl_close($curl);

    echo $data;*/

    //same
    $url = 'http://www.dev-c.com/nativedb/reference.html';
    $file = basename($url);

    $fp = fopen($file, 'w');

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FILE, $fp);

    $data = curl_exec($ch);

    curl_close($ch);
    fclose($fp);
?>

因此,如果我使用这些代码中的任何一个,输出都是空的,并且在我的网络服务器上创建的文件是空的。 文件:http://www.dev-c.com/nativedb/reference.html

我提取了您在示例中提供的用于网络 URL 的 headers。这是我得到的:

HTTP/1.1 200 OK Server: nginx/1.12.0 Date: Fri, 23 Jun 2017 11:34:58 GMT Content-Type: application/octet-stream **Content-Length: 0** Connection: keep-alive Content-Description: reference.html Content-Disposition: attachment; filename=reference.html Content-Transfer-Encoding: binary Expires: Thu Jun 22 11:34:58 2017 GMT Cache-Control: must-revalidate Pragma: public Set-Cookie: PHPSESSID=f9d4eb515eeabfb461a11d056fdc7b78; path=/ Cache-Control: max-age=3600, private, proxy-revalidate

如您所见,Content-Length:0 为零。这可能是您得到空 body.

的原因之一

如果您有权访问承载 reference.html 的 NGINX 服务器,您可以在尝试下载 reference.html.[=11 时尝试转储 /var/log/nginx/error.log 的内容=]

如果 Content-Length 为零,则没有可供下载的文件大小。它指出服务器设置存在问题。

谢谢您的帮助,但问题出在编码上。 我必须发送 header "Accept-Encoding: gzip" 然后 gzdecode 输出。

<?php
error_reporting(E_ALL);

$url = 'http://www.dev-c.com/nativedb/reference.html';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Host: www.dev-c.com",
"Accept-Encoding: gzip"
));

$data = curl_exec($curl);

curl_close($curl);

echo gzdecode($data);
?>