我试图了解curl多渠道的工作原理。看了几个位置,包括stackoverflow,我现在有了以下代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, MY_STREAM_URL);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, "true");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, MY_LOGIN.":".MY_PWD);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'get_stream');
curl_setopt($ch, CURLOPT_BUFFERSIZE, 2000);
curl_setopt($ch, CURLOPT_LOW_SPEED_LIMIT, 1);
curl_setopt($ch, CURLOPT_LOW_SPEED_TIME , 120);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$running = null;
$mh = curl_multi_init();
curl_multi_add_handle($mh, $ch);
do {
curl_multi_select($mh, 1);
curl_multi_exec($mh, $running);
} while($running > 0);
curl_multi_remove_handle($mh, $ch);
curl_multi_close($mh);
function get_stream($ch, $data) {
$length = strlen($data);
echo 'Received '.$length.'B on '.date('Y-M-d H:i:s T');
return $length;
}
我遇到的问题是,虽然这有效,但我并不确定如何让它们变得更好(并且卷曲PHP手册没有我正在寻找的细节)。< / p>
我需要做的是获取数据,并在累积数据达到1MB时将其写入文件,然后再使用新文件重新开始。我确定一旦弄清楚了,我就会说'#34; duh&#34;,但与此同时,我有点困惑。
任何指导都将不胜感激。
function get_stream($ch, $data) {
global $cum_data_size;
global $data_buffer;
$length = strlen($data);
echo 'Received '.$length.'B at '.date('Y-M-d H:i:s T');
$cum_data_size = $cum_data_size + $length;
$data_buffer .= $data;
if ($cum_data_size >= 1000000) {
write_out_data_file();
}
return $length;
}
function write_out_data_file ()
{
global $cum_data_size;
global $data_buffer;
$handle = fopen(DATA_FLDR.time().'_.gzip', 'w+');
fwrite($handle, $data_buffer);
fclose($handle);
$data_buffer = '';
$cum_data_size = 0;
}