curl: How to limit size of GET?

后端 未结 5 770
逝去的感伤
逝去的感伤 2020-12-11 22:03

I want to retrieve the first 10k bytes from a URL with curl (using PHP in my case). Is there a way to specify this? I thought CURLOPT_BUFFERSIZE would do this, but it just a

5条回答
  •  醉酒成梦
    2020-12-11 22:17

    $html='';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
    curl_setopt($ch, CURLOPT_NOPROGRESS, false);
    curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function($DownloadSize, $Downloaded, $UploadSize, $Uploaded){ return ($Downloaded > 10240) ? 1 : 0;});
    curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'write_function');
    curl_exec($ch);
    curl_close($ch);
    echo $html;
    
    function write_function($handle, $data) {
        global $html;
        $html .= $data;
        if (strlen($html) > 10240) {
            return 0;
        }
        else
            return strlen($data);
    }
    

提交回复
热议问题