2010-01-10 6 views
0

왜이 코드가 멈 춥니 까? break 문을 주석으로 처리하면 제대로 작동하지만 $ sitelist 배열의 첫 번째 항목에 대해서만 작동합니다.PHP 컬 질문

필자는 PHP 컬의 다중 사이트 기능에 대해 알고 있지만 병렬로 파일을 가져 오는 것에는 관심이 없습니다.

코드 스 니펫 : 당신이 foreach 루프의 끝 부분에 이미있는 한

$sitelist = array(11204,11509,20602,21601,22003,22704,23106,23303,23402,23422,23427,30215,30220,30234,30282,30302,30304,30905,40110,40122,40204,40406,40624,50144,50304,50408,50409,50801,50807,50904,51001,51502,52212,52219,52233,52604,52606,60703,61020,61022,61024,61502,61602,61901,61905,61909,62102,62114,62803,72301,73503,73801,74903); 

$actliste = array(); 
$recherche =' <a name="JOUR1">'; 

foreach ($sitelist as $site) 
{ 

    # obtain the data at each site 

    // Create a curl handle 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    $url = sprintf("http://www.cehq.gouv.qc.ca/Suivihydro/tableau.asp?NoStation=0%s", $site); 
    //echo $url . '<br />'; 

    curl_setopt($ch, CURLOPT_URL, $url); 

    $output = curl_exec($ch); // Run curl 
    if ($output === FALSE) 
    { 
     echo 'Curl error retrieving: ' . $url. ' ' . curl_error($ch) . '<br />'; 
     exit; 
    } 

    // Set up a way to detect HTTP anomolies. An HTTP code of 200 is expected 
    $info = curl_getinfo($ch); 
    if ($info['http_code'] <> 200) 
    { 
     echo sprintf("HTTP error code %s returned\n", $info['http_code']); 
     echo strip_tags(sprintf("Message received: %s\n", $output)); 
     exit($info['http_code']); // The HTTP error code is returned so it can be grabbed programatically 
    } 

    echo '0' . $site; 

    // Find: first <td width="33%" until the first </tr> 
    $start_loc = strpos($output, '<td width="33%"'); 
    $end_loc = strpos($output, '</tr>', $start_loc); 
    $substring = substr($output,$start_loc, $end_loc - $start_loc); // Contains all the data we need 

    // Get day 
    $day_pos = strpos($substring, '<font face="Verdana" size="2">') + 30; 
    //echo "Substring = " . $substring . '<br />'; 
    $day = substr($substring,$day_pos,10); // 2010-01-08 
    echo ' ' . $day; 

    // Get time 
    $time_pos = strpos($substring,'<font face="Verdana" size="2">', $day_pos) + 30; 
    $time = substr($substring, $time_pos, 5); 
    echo ' ' . $time; 

    // Get measurement 
    $measurement_pos = strpos($substring,'<font face="Verdana" size="2">', $time_pos) + 30; 
    $measurement = substr($substring, $measurement_pos, 5); 
    echo ' ' . $measurement . '<br />'; 

    curl_close($ch); 
    echo 'Channel closed<br />'; 

    sleep(2); 
    echo 'Done sleeping'; 
    //break; 

} 
+0

"아주 오래 걸릴만큼 매달리는 것처럼 보이지 않지만"속도를 높이는 가장 좋은 방법은 "관심이 없다"고 말하면 (아래)? – GZipp

+0

매우 오랜 시간이 걸릴 정도로 교수형에 처하는 것처럼 보이지 않습니다. 아마 나는 이것을 다시 말해야 할 것이다 : 전체 스크립트가 끝나기 전에 출력물을 반향시키고 싶다. – Mark

답변

1

당신은 전혀 휴식이 필요하지 않습니다.

매달려있는 이유는 각 루프 끝에 flush 출력을 넣을 수 있기 때문입니다. 현재 break 인 경우 flush()을 입력하십시오.

+0

감사. flush()를 추가해도 프로세스가 빨라지지는 않지만 중요하지는 않습니다. 이 데이터를 수집하는 가장 좋은 방법은 cron 작업으로 로컬 캐시의 값을 제공하는 것입니다. – Mark