在PHP中,我希望继续处理从cURL中使用CURLOPT_WRITEFUNCTION
处理程序下载的大型XML文件。我不完全确定如何确定何时文件已完成下载。
任何人都可以帮忙吗?是我在回调函数中应用的逻辑吗?或者是否有我无法找到的onComplete回调函数?
以下是代码段的一部分:
private function getData(){
// Set up cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FILE, $this->xmlFile);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, -1);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
// Assign a callback function to the CURL Write-Function
curl_setopt($ch, CURLOPT_WRITEFUNCTION, array($this, 'curlWriteFile'));
// Exceute the download - note we DO NOT put the result into a variable!
curl_exec($ch);
// Fail on cURL error and log it
if($e = curl_errno($ch)){
// Handle Errors
}else{
// No Errors
// This is where I would call the next parts of the process
}
// Close CURL
curl_close($ch);
// Close the file pointer
fclose($this->xmlFile);
}
private function curlWriteFile($cp, $data) {
// Write the xml data chunk to a file
$len = fwrite($this->xmlFile, $data);
// If we do not return the file length in bytes, the curl callback fails!
return $len;
}