PHP卷曲问题

时间:2010-01-10 00:18:15

标签: php curl

为什么这段代码会挂起?注释掉break语句并且它工作正常,但仅适用于$ sitelist数组中的第一项。

我了解PHP curl的多站点功能,但我对并行获取文件不感兴趣。

代码段:

$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;

}

1 个答案:

答案 0 :(得分:1)

你根本不需要休息,因为你已经在foreach循环结束了。

至于它挂起的原因,你可以在每个循环结束时flush输出。如果您目前有break,请放置flush()