简体   繁体   中英

CURLOPT_FILE don't write headers

I need to download a large file and also need to get the headers in order to get the redirect location (status code 302).

I use this:

curl_setopt($ch, CURLOPT_FILE, $file);
curl_setopt($ch, CURLOPT_HEADER, true);

Now the problem is the following:

It is writing the header data as well to the output file.

Is there a way to only write the content?

Edit:

Just to make it clear. All i want is to get the headers, but i want that only the body get's written to the file. Also please keep in mind that i can't store the body data into a variable since its a video data and therefore to big (memory_limit).

With the help of @Bishop i was able to get the working solution:

    $url = 'example.com';
    $file = fopen('body.txt', 'w');
    $file_header = fopen('headers.txt', 'w')
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_WRITEHEADER, $file_header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
    curl_setopt($ch, CURLOPT_FILE, $file);
    $data = curl_exec($ch);

Now i have the header in a seperate file and can get it from there by using:

    file_get_contents('headers.txt');

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