0

Why does this code hang? Comment out the break statement and it works fine, but only for the first item in the $sitelist array.

I know about PHP curl's multisite feature, but I am not interested in fetching files in parallel.

Code snippet:

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

}
2
  • It appears not so much to hang as to take a very long time. Perhaps I should rephrase this: I would like it to echo output before the whole script ends. Commented Jan 10, 2010 at 0:21
  • You say (below) that "It appears not so much to hang as to take a very long time", but you're "not interested" in the best way to speed it up? Commented Jan 10, 2010 at 2:24

1 Answer 1

1

You don't need the break at all, as you're already at the end of the foreach loop.

As for why it's hanging, you can flush the output at the end of each loop. Where you currently have break, put flush().

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Adding flush() didn't speed up the process, but it doesn't matter. I'm thinking the best way to collect this data is with a cron job then serve the values from a local cache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.