curl php 响应 204 下载 zip 文件

curl php response 204 to download zip file

我需要将文件下载到远程服务器。但是我不能用 php 中的 curl 来做到这一点。 代码是:

$ch = curl_init($source);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_BUFFERSIZE, 8096);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        var_dump(curl_exec($ch));

        print_r(curl_getinfo($ch));
        die;

结果是:

string(0) ""
Array
(
    [url] => http://example.com/example.zip
    [content_type] => 
    [http_code] => 204
    [header_size] => 213
    [request_size] => 118
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 1.180951
    [namelookup_time] => 0.012276
    [connect_time] => 0.105478
    [pretransfer_time] => 0.105523
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 1.18091
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 104.79.246.86
    [certinfo] => Array
        (
        )

    [primary_port] => 80
    [local_ip] => 192.168.0.14
    [local_port] => 46676
)

文件在浏览器中正常下载。怎么了?。对不起我的英语,谢谢你的帮助

我已经尝试了您的基本示例并且它正在运行(收到带有标准 zip public 文件的代码 200)。

我认为服务器可能正在过滤可疑请求。尝试通过设置 headers:

来模拟浏览器

示例:

$url="https://new.aol.com/productsweb/subflows/ScreenNameFlow/AjaxSNAction.do?s=username&f=firstname&l=lastname";
$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
var_dump($result);

来源:php curl: how can i emulate a get request exactly like a web browser?

204 状态表示服务器选择不return任何内容,只是更新元数据。这样做可能是因为它正在检查您使用的是哪种浏览器,并且未设置为响应 curl。

查看 How to disguise your PHP script as a browser? 了解如何使您的脚本看起来像浏览器。

顺便说一句,你只需要设置一次CURLOPT_RETURNTRANSFER。将其设置为 true 或 1 会得到相同的结果。