HuyPV
Wednesday, March 28, 2012
<?php
$time_start = microtime(true);
##############################
$curl = curl_init();
$data = array(
'x' => 'vrand_' . uniqid(),
'FieldData1' => 'ok'
);
curl_setopt($curl, CURLOPT_URL, 'http://dantri.com.vn/');
curl_setopt($curl, CURLOPT_VERBOSE, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$nbRetry = 0;
$hasResult = false;
define('MAX_RETRY', 100);
define('IDLE_TIME', 3);
while (!$hasResult && $nbRetry <= MAX_RETRY) {
$result = curl_exec($curl);
if (curl_errno($curl)) {
if ($nbRetry == 0) {
echo 'ERR: ' . curl_error($curl) . PHP_EOL;
} else {
echo 'ERR (retry#' . str_pad($nbRetry, strlen(MAX_RETRY), '0', STR_PAD_LEFT) . '): ' . curl_error($curl) . PHP_EOL;
}
$nbRetry++;
sleep(3);
} else {
$http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if (false || $http_status == 404) {
# for special case, if we only expected 200 example
if ($nbRetry == 0) {
echo 'ERR: Not like we exptected!' . PHP_EOL;
} else {
echo 'ERR (retry#' . str_pad($nbRetry, strlen(MAX_RETRY), '0', STR_PAD_LEFT) . '): Not like we exptected!' . PHP_EOL;
}
$nbRetry++;
sleep(IDLE_TIME);
} else {
if ($nbRetry == 0) {
echo 'OK: ' . $http_status . PHP_EOL;
} else {
echo 'After ' . $nbRetry . ' times retry, we have result as expected ^^: ' . $http_status . PHP_EOL;
}
$hasResult = true;
}
}
}
if (!$hasResult) {
echo 'After ' . MAX_RETRY . ' times retry, we have no result #.# --> QUIT' . PHP_EOL;
}
##############################
$time_end = microtime(true);
$time = $time_end - $time_start;
$secs = $time;
echo 'Done! ' . sprintf('%02u:%02u:%02u', $secs / 3600, $secs % 3600 / 60, $secs % 60) . ' seconds';
Title:
PHP cURL reconnect to check site status
Description:
<?php $time_start = microtime(true); ############################## $curl = curl_init(); $data = array( 'x' => 'vran...
...
Rating:
4