2011-08-26 3 views
0

file1.php curl이 file2.php를 실행하는 거래가 있습니다. file2.php는 장시간 실행되는 파일이지만, 응답을 file1.php에 보내고 (또는 전송할 예정입니다.) 코드를 수행합니다. 이 데이터를 전송하려고 출력 버퍼를 사용하고 있지만 문제는 '돌아 오는 경우'입니다. 플러시 직후. file1.php는 응답을 잘 받지만, file2.php를 계속 실행하려고하면 file1.php는 응답을받지 못합니다. 내가 잘못하고있는 것은 무엇입니까? file1.php에 응답을 다시 보내야하는 다른 방법이 있습니까?PHP Curl 출력 버퍼가 응답을 수신하지 않음

// file1.php 
    $url = "file2.php" 

    $params = array('compurl'=>$compurl,'validatecode'=>$validatecode); 

    $options = array(
     CURLOPT_RETURNTRANSFER => true,  // return web page 
     CURLOPT_HEADER   => true,  // return headers 
     CURLOPT_FOLLOWLOCATION => true,  // follow redirects 
     CURLOPT_ENCODING  => "",  // handle all encodings 
     CURLOPT_USERAGENT  => "Mozilla", // who am i 
     CURLOPT_AUTOREFERER => true,  // set referer on redirect 
     CURLOPT_CONNECTTIMEOUT => 120,  // timeout on connect 
     CURLOPT_MAXREDIRS  => 10,  // stop after 10 redirects 
     CURLOPT_TIMEOUT  => 10,  // don't wait too long 
     CURLOPT_POST   => true,  // Use Method POST (not GET) 
     CURLOPT_POSTFIELDS  => http_build_query($params) 
    ); 
    $ch = curl_init($url); 

    curl_setopt_array($ch, $options); 
    $response = curl_exec($ch); 
    curl_close($ch); 
    echo $response; 

// file2.php 
ob_start(); 
echo 'Running in the background.'; 

// get the size of the output 
$size = ob_get_length(); 

header("HTTP/1.1 200 OK"); // I have tried without this 
header("Date: " . date('D, j M Y G:i:s e')); // Tried without this 
header("Server: Apache"); // Tried without this 
header('Connection: close'); 
header('Content-Encoding: none'); 
header("Content-Length: $size"); 
header("Content-Type: text/html"); // Tried without this 

// flush all output 
ob_end_flush(); 
ob_flush(); 
flush(); 

// If I add return; here file1.php gets the response just fine 
// But I need file2.php to keep processing stuff and if I remove the 
// return; file1.php never gets a response. 

답변

3

정상적인 컬 전달에서는 페이지로드가 완료 될 때까지 데이터를 가져올 수 없습니다. 귀하의 스크립트가 완료되었습니다. 부분 데이터로 작업하려면 CURLOPT_WRITEFUNCTION을 확인해야합니다. 이렇게하면 콜백이 생성되어 데이터를 사용할 수있을 때마다 사용할 수 있습니다.

+0

그러나 또 다른 stackoverflow 질문에 이것이 가능함이 표시됩니다 : http://stackoverflow.com/questions/2996088/how-do-you-run-a-long-php-script-and-keep-sending- updates-to-the-browser-via-http –

+0

나는 CURLOPT_WRITEFUNCTION을 시도했지만 여전히 응답을 읽지 못했습니다 .. 어떻게 부분 응답을 기다리고 스크립트를 종료하지 않도록 루프 할 수 있습니까? 자원을 묶어 라? –

+0

컬은 브라우저가 아닙니다. 브라우저는 부분적인 데이터를 표시 할 수 있지만 페이지가 완료된 후에 만 ​​말릴 수 있습니다. 솔직히 내가 멀티 스레딩을 사용하지 않는 한 함수가 부분 데이터를 반환 할 수있는 방법을 이미징 할 수 없습니다. 부분 데이터를 사용하려면 CURLOPT_WRITEFUNCTION이 제공하는 콜백이 필요합니다. – Ravi