3

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;
}
mpromonet
  • 11,326
  • 43
  • 62
  • 91
MightyQ
  • 61
  • 5
  • 2
    curl_exec blocks until the request completes. just don't use the file until AFTER the curl_exec() call – Marc B Oct 14 '15 at 15:19
  • Strangely enough, I was using XMLReader class on that file, and what was happening is that the reader started reading the file faster than cURL was writing the file. In a larger file, it caught up and starting creating xml parse errors. It feels like the method was still running even after curl_exec had run... ? – MightyQ Oct 14 '15 at 15:23

0 Answers0