PHP curl question
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,6102开发者_StackOverflow0,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;
}
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()
.
精彩评论