3

在 PHP 中,我希望继续处理从使用CURLOPT_WRITEFUNCTIONcURL 中的处理程序下载的大型 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;
}
4

0 回答 0