Php 帮助我理解CURLOPT_READFUNCTION

Php 帮助我理解CURLOPT_READFUNCTION,php,curl,libcurl,Php,Curl,Libcurl,我想正确理解CURLOPT_READFUNCTION 我正在看Rackspace coudfiles php代码(RESTAPI) 它有以下几行 curl_setopt($ch, CURLOPT_READFUNCTION, array(&$this, '_read_cb')); 查看此函数的定义: private function _read_cb($ch, $fd, $length) { $data = fread($fd, $length); $len = strl

我想正确理解CURLOPT_READFUNCTION

我正在看Rackspace coudfiles php代码(RESTAPI)

它有以下几行

curl_setopt($ch, CURLOPT_READFUNCTION, array(&$this, '_read_cb'));
查看此函数的定义:

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;
}
您能帮助我了解传递给$fd和$length的值吗

我想特别指定$length值,以分块发送文件

提前感谢。

此处的内容似乎有误:

CURLOPT_READFUNCTION
回调函数,其中回调函数 函数接受两个参数。这个 首先是cURL资源,然后是 第二个是一个字符串,其中包含要处理的数据 阅读必须使用以下命令读取数据: 这是一个回调函数。归还 读取的字节数。将0返回到 信号EOF

它实际上需要三个参数(请参见):

  • 第一个是卷曲手柄
  • 第二个是通过选项
    CURLOPT\u infle
    设置的PHP流
  • 第三个是应该从PHP流中读取并传递到curl库的数据量,以便它可以将其发送到HTTP服务器

编辑:修复了这个问题。

我知道这有点像死尸——但其他人可能想知道它是如何工作的

下面是典型的curl file put块的外观:

$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);
read函数看起来像这样(这个函数限制用户定义的$goal速度,并提供CLI显示反馈)

函数curlPutThrottle($ch,$fh,$length=false)
{
全球美元规模;
全球流动美元;
全球金融危机;
全球美元启动;
/**设置最大上传速度-此处为30mB/分钟**/
$goal=(300*1024*1024)/(60*10);
如果(!$length)
{
$length=1024*1024;
}
如果(!是_资源($fh))
{
返回0;
}
$current+=$length;
如果($current>$throttle)/**每个上传的meg,我们会更新显示并稍微调节一下以达到目标速度**/
{
$pct=圆形($current/$size*100);
$disp=“上传(“.$pct.”%)-”.number_格式($current,0)。“/”.number_格式($size,0);
echo“\r”。$disp.str_repeat(“,strlen($disp));
$throttle+=1024*1024;
$appeased=time()-$start;
$expectedUpload=$goal*$Appead;
如果($current>$expectedUpload)
{
$sleep=($current-$expectedUpload)/$goal;
$sleep=圆形($sleep);
对于($i=1;$i$size)
{
回音“\n”;
}
返回fread($fh,$length);
}
其中:

  • $ch是调用ReadFunction的cURL资源
  • $fh是CURLOPT_infle的文件句柄
  • $length是它希望返回的数据量

它从$length或“”文件返回数据(如果EOF)。

使用curl,您可以读取URL,而不是发送块。您在尝试什么?有趣。这里的手册是错误的,它说回调只需要两个参数。我正在尝试上载大文件(约2 GB)使用此API但失败了。可能是由于共享服务器上的内存限制。有没有办法读取小块数据并发送文件?(比如读取20mb数据,发送,循环直到EOF。我知道这可以使用fopen/fread完成,但我想知道是否可以使用此curl选项)@KingCrunch-我正在尝试将文件发送/上载到某个URL
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);
}