简体   繁体   中英

How to use stream_notification_callback with cURL

Is it possible to use stream_notification_callback with cURL?
I would like to adapt the example #1 that I have found here http://www.php.net/manual/en/function.stream-notification-callback.php , to my cURL function below to create/update a text file that contains the downloaded bytes.

I know that CURLOPT_PROGRESSFUNCTION is implemented in php 5.3 but I'm running php 5.2 and I can't upgrade.

private function Save($url) {
    $this->SetTempFileName(time());
    $file = fopen($this->GetTempVidFileName(), 'w');
    $ckfile = tempnam("/tmp_cookie", "CURLCOOKIE");
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_FILE, $file);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
    curl_exec($ch);
    curl_close($ch);
    fclose($file);
    return is_file($this->GetTempFileName());
}

I know that I will have to use file_put_contents to replace the "case STREAM_NOTIFY_PROGRESS" part like this...

case STREAM_NOTIFY_PROGRESS:
file_put_contents('progress.txt', $bytes_transferred);
break;

...but my question is how to adapt the two codes? Thanks in advance.

I'm afraid your options are either not to use curl (and use the much more limited HTTP wrapper instead or even sockets) or upgrade.

PHP 5.2 is dead (not more updates, even security fixes). You can also try to compile PHP 5.3 for PHP 5.2 (there will likely be only a few easy issues) or even backport the CURLOPT_PROGRESSFUNCTION support.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM