cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

Re: curl partial HTTP request

From: Shailesh N. Humbad <humbads_at_alum.mit.edu>
Date: Wed, 26 Mar 2008 16:35:48 -0500

> On Mon, 24 Mar 2008, Shailesh N. Humbad wrote:
>> > Is it possible to make a partial HTTP request using Curl? This is when
>> > making an HTTP GET request a server that does not support the HTTP range
>> > option and does not return the file size in the HTTP headers, so the range
>> > and max-filesize options won't work.
>> >
>> > With fopen/fread, I can read a fixed number of bytes from the request and
>> > then close the file handle. I want to accomplish the same thing with curl,
>> > but can't find an option to do that. The option would allow me to read only
>> > up to X bytes from the response, and then forcibly close the connection.
>
> I think the PHP binding to libcurl supports the write callback, and if that
> resembles the "real" libcurl's function it can abort the download at any point
> it deems necessary.

Thanks Daniel, that worked. Here's a rough solution to do a partial
HTTP GET request in PHP:

$GLOBALS["dataread"] = 0;
define("MAX_DATA", 3000);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch, CURLOPT_WRITEFUNCTION, "handlewrite");
curl_exec($ch);
curl_close($ch);

function handlewrite($ch, $data)
{
        $GLOBALS["dataread"] += strlen($data);
        echo "READ ".strlen($data)." bytes\n";

        if($GLOBALS["dataread"] > MAX_DATA) {
                return 0;
        }
        
        return strlen($data);
}
_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2008-03-26