3

我想知道是否可以在使用时调用的函数中添加自定义参数

curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, array($this, 'progressFunction'));

所以函数签名看起来像

private function progressFunction($clientp, $dltotal, $dlnow, $ultotal, $ulnow, $myCustomParameter) { }

代替

private function progressFunction($clientp, $dltotal, $dlnow, $ultotal, $ulnow) { }

谢谢。

4

2 回答 2

10

好的,这严格来说是一个建议,我会将其作为答案,因为我认为它可能会起作用;

PHP 支持匿名函数,所以你可以像这样传递它:

curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function ($clientp, $dltotal, $dlnow, $ultotal, $ulnow) {

});

现在诀窍是使用以下方法传递您的参数use

curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function (...) use ($myCustomParameter) {
    // Your code
    });
于 2013-01-11T21:11:08.007 回答
0

在 PHP 类中,您可以将其他参数传递$statusName给另一个函数,如下所示:

curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function ($resource, $downloadSize, $downloaded, $uploadSize, $uploaded) use ($statusName) {
    $this->progress($resource, $downloadSize, $downloaded, $uploadSize, $uploaded, $statusName);
});

然后,您专用的进度功能不会使代码变得更加丑陋:

private function progress($resource, $downloadSize, $downloaded, $uploadSize, $uploaded, $statusName="curl_download_status")
{
    // emit the progress    
    Cache::put($statusName, [
        'resource' => $resource,
        'download_size' => $downloadSize,
        'downloaded' => $downloaded,
        'upload_size' => $uploadSize,
        'uploaded' => $uploaded
    ], 10);

}
于 2017-06-05T22:11:14.380 回答