2014-04-06 2 views
0

첫 번째 스크립트에 대한 결과 만 표시됩니다. 그러나 두 PHP 스크립트 모두에 대한 응답을 하나의 출력으로 보내고 싶습니다. 내가 개발중인 프로그램에서 여러 원격 서버를 쿼리하고 있습니다. 다른 서버에서 정보를 검색하고 그 결과를 하나의 응답으로 반환해야합니다.2 개의 PHP 스크립트 출력 결과를 병합하는 방법

<?php 
    // build the individual requests as above, but do not execute them 
    $ch_1 = curl_init('http://lifesaver.netai.net/example/pharm.php'); 
    $ch_2 = curl_init('http://192.168.1.2/example/pharm.php'); 
    curl_setopt($ch_1, CURLOPT_RETURNTRANSFER, false); 
    curl_setopt($ch_2, CURLOPT_RETURNTRANSFER, false); 
    curl_setopt($ch_1, CURLOPT_POST,1); 
    curl_setopt($ch_2, CURLOPT_POST,1); 
    curl_setopt($ch_1, CURLOPT_POSTFIELDS, "name=$value"); 
    curl_setopt($ch_2, CURLOPT_POSTFIELDS, "name=$value"); 

    // $_POST["name"] 

    // build the multi-curl handle, adding both $ch 
    $mh = curl_multi_init(); 
    curl_multi_add_handle($mh, $ch_1); 
    curl_multi_add_handle($mh, $ch_2); 

    // execute all queries simultaneously, and continue when all are complete 
    $running = null; 
    do { 
     curl_multi_exec($mh, $running); 
    } while ($running); 

    // all of our requests are done, we can now access the results 
    $response_1 = curl_multi_getcontent($ch_1); 
    $response_2 = curl_multi_getcontent($ch_2); 

    echo $response_1; 
    echo $response_2; 
?> 

답변

0

curl_multi_getcontent의 문서에 따르면, 당신은 true 대신 falseCURLOPT_RETURNTRANSFER을 설정해야합니다. 이 실행 루프에 관해서

또한, curl_multi_init의 문서는 당신보다 약간 다른 예를 보여줍니다

//execute the handles 
$running = null; 
do { 
    $mrc = curl_multi_exec($mh, $running); 
} while ($mrc == CURLM_CALL_MULTI_PERFORM); 

while ($running && $mrc == CURLM_OK) { 
    if (curl_multi_select($mh) != -1) { 
     do { 
      $mrc = curl_multi_exec($mh, $running); 
     } while ($mrc == CURLM_CALL_MULTI_PERFORM); 
    } 
} 

당신은 그런 식으로 시도 할 수 있습니다.

관련 문제