curl php response 204下载zip文件

时间:2015-06-11 14:20:56

标签: php curl remote-server

我需要将文件下载到远程服务器。但是我不能用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
)

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

2 个答案:

答案 0 :(得分:0)

我已经尝试了你的基本示例,它正在运行(收到带有标准zip公共文件的代码200)。

我认为服务器可能正在筛选可疑请求。尝试通过设置标题来模拟浏览器:

示例:

$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?

答案 1 :(得分:0)

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

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

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