Help me understand CURLOPT_READFUNCTION

I want to understand CURLOPT_READFUNCTION correctly.

I am looking at the Rackspace coudfiles PHP code (REST API).

It has the following line.

curl_setopt($ch, CURLOPT_READFUNCTION, array(&$this, '_read_cb')); 

Looking to protect this feature:

 private function _read_cb($ch, $fd, $length) { $data = fread($fd, $length); $len = strlen($data); if (isset($this->_user_write_progress_callback_func)) { call_user_func($this->_user_write_progress_callback_func, $len); } return $data; } 

Can you help me understand what values ​​are passed in $ fd and $ length?

I want to specifically specify a length value of $ to send the file to pieces.

Thanks in advance.

+4
source share
2 answers

I know this is a little necro, but other people may want to find out how it works.

Here, as a normal twisted file addition unit would look like:

 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ret['Location']); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_PUT, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_READFUNCTION, 'curlPutThrottle'); curl_setopt($ch, CURLOPT_INFILE, $fh); curl_setopt($ch, CURLOPT_INFILESIZE, $size); $ret = curl_exec($ch); 

and the read function will look something like this (this throttle is dependent on the user speed $ goal and gives feedback to the CLI display)

 function curlPutThrottle($ch, $fh, $length = false) { global $size; global $current; global $throttle; global $start; /** Set your max upload speed - here 30mB / minute **/ $goal = (300*1024*1024)/(60*10); if (!$length) { $length = 1024 * 1024; } if (!is_resource($fh)) { return 0; } $current += $length; if ($current > $throttle) /** Every meg uploaded we update the display and throttle a bit to reach target speed **/ { $pct = round($current/$size*100); $disp = "Uploading (".$pct."%) - ".number_format($current, 0).'/'.number_format($size, 0); echo "\r ".$disp.str_repeat(" ", strlen($disp)); $throttle += 1024*1024; $elapsed = time() - $start; $expectedUpload = $goal * $elapsed; if ($current > $expectedUpload) { $sleep = ($current - $expectedUpload) / $goal; $sleep = round($sleep); for ($i = 1; $i <= $sleep; $i++) { echo "\r Throttling for ".($sleep - $i + 1)." Seconds - ".$disp; sleep(1); } echo "\r ".$disp.str_repeat(" ", strlen($disp)); } } if ($current > $size) { echo "\n"; } return fread($fh, $length); } 

Where:

  • $ ch - cURL resource that calls the ReadFunction function
  • $ fh is the file descriptor from CURLOPT_INFILE
  • $ length is the amount of data that it expects to receive.

It returns data from a file of length $ length or `` if EOF.

+5
source

the guide here is wrong:

CURLOPT_READFUNCTION The name of the callback function, where the callback function takes two parameters. firstly, it is a cURL resource, and the second line with data to read. Data must be read using this callback function. Return the number of bytes read. Return 0 to EOF signal.

In fact, it takes three parameters (see source code ):

  • The first is the curl pen.
  • The second is the PHP stream, which is set using the CURLOPT_INFILE option.
  • The third is the amount of data that needs to be read from the PHP stream and passed to the curl library so that it can send it to the HTTP server.

UPDATE: Fixed in this commit .

+3
source

Source: https://habr.com/ru/post/1347515/


All Articles