Diğer cevaplar dediği gibi:
- Eğer bellekte tüm bu olamaz
- çözeltisi kullanmak olacaktır
CURLOPT_FILE
Ama, gerçekten bir dosya ne oluşturmak için olmayabilir; Eğer kısa sürede "geldiğinde" olarak kullanarak ... bellek veri ile çalışmak istiyor olabilir.
Olası bir çözüm akışı sarıcı kendi definind ve CURLOPT_FILE
ile, yerine gerçek bir dosya, bu birini kullanın olabilir
Her şeyden önce, bkz
And now, let's go with an example.
Birincisi, bizim akım sarıcı sınıfı yapalım:
class MyStream {
protected $buffer;
function stream_open($path, $mode, $options, &$opened_path) {
// Has to be declared, it seems...
return true;
}
public function stream_write($data) {
// Extract the lines ; on y tests, data was 8192 bytes long ; never more
$lines = explode("\n", $data);
// The buffer contains the end of the last line from previous time
// => Is goes at the beginning of the first line we are getting this time
$lines[0] = $this->buffer . $lines[0];
// And the last line os only partial
// => save it for next time, and remove it from the list this time
$nb_lines = count($lines);
$this->buffer = $lines[$nb_lines-1];
unset($lines[$nb_lines-1]);
// Here, do your work with the lines you have in the buffer
var_dump($lines);
echo '<hr />';
return strlen($data);
}
}
Ben ne olduğunu:
- Ulaştığında (ben var_dump kullanmak, ancak bunun yerine her zamanki şeyler yapardım) veri parçaları üzerinde çalışmak
- Eğer "dolu satırları" alamadım Not: Bir satır sonunda bir yığın bir başlangıcı olduğunu ve aynı hattın başlangıcı önceki öbek sonunda oldu; bu yüzden, size çağrıları arasında bir chunck bazı parçaları tutmak zorunda
stream_write
Next, we register this stream wrapper, to be used with the pseudo-protocol "test" :
// Register the wrapper
stream_wrapper_register("test", "MyStream")
or die("Failed to register protocol");
And, now, we do our curl request, like we would do when writting to a "real" file, like other answers suggested :
// Open the "file"
$fp = fopen("test://MyTestVariableInMemory", "r+");
// Configuration of curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.rue89.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 256);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $fp); // Data will be sent to our stream ;-)
curl_exec($ch);
curl_close($ch);
// Don't forget to close the "file" / stream
fclose($fp);
Gerçek bir dosya ile çalışmak, ama bizim sözde protokolü ile gerekmez.
This way, each time a chunk of data arrives, MyStream::stream_write
method will get called, and will be able to work on a small amount of data (when I tested, I always got 8192 bytes, whatever value I used for CURLOPT_BUFFERSIZE
)
A few notes :
- Ben mi daha açıkçası, bu daha fazla test etmek gerekir
- çizgiler 8192 bytetan ise benim stream_write uygulanması muhtemelen çalışmaz; kadar bunu yama için ;-)
- Sadece birkaç işaretçiler, ve bir tam çalışma çözüm olarak anlatıyor: (tekrar) test ve muhtemelen biraz daha fazla kod var!
Still, I hope this helps ;-)
Have fun !