In PHP, I wish to continue processing a large XML file that has downloaded from using the CURLOPT_WRITEFUNCTION
handler in cURL. I am not entirely certain how to determine when the file has completed downloading.
Can anyone help with this? Is it logic I apply in my callback function? Or is there a callback function for onComplete that I am unable to find?
Here is a part of the code snippet:
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;
}