curl php response 204 to download zip file

1.4k views Asked by At

I need a download a file to remote server. But i can't do it with curl in php. The code is:

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

The result is:

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
)

The file download normaly in browser. what is wrong?. Sorry for my English and thanks for your help

2

There are 2 answers

0
Beth Macknik On BEST ANSWER

A 204 status means the server is choosing to not return any content, just updated meta data. It may be doing that because it is checking for what kind of browser you are using and isn't setup to respond to curl.

Check out How to disguise your PHP script as a browser? for how to make your script look like a browser.

BTW, you only need to set CURLOPT_RETURNTRANSFER once. Setting it to true or 1 will give you the same result.

0
manuelbcd On

I have tried your basic example and it is working (received code 200 with standard zip public files).

I think the server could be filtering suspecting requests. Try to emulate browsers by setting headers:

Example:

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

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