Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use stream_notification_callback with cURL

Tags:

php

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.

like image 477
Maxime Avatar asked Nov 13 '22 23:11

Maxime


1 Answers

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.

like image 90
Artefacto Avatar answered Dec 28 '22 15:12

Artefacto