如何最小化这个PHP脚本

时间:2018-01-13 18:20:23

标签: php curl

您好我设计了一个页面,其中PHP检查网站状态,如果它获得200个标题状态,那么它显示网站已启动,否则显示网站已关闭。

我有大约5个网站,为此,我必须制作单独的脚本,检查下面的代码。

<!DOCTYPE html>
<html lang="en">
<head>
<title>Status Page
</title>
</head>
<body>
<?php
$URL  = 'https://www.example1.com';
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $URL);
curl_setopt($curlHandle, CURLOPT_HEADER, true);
curl_setopt($curlHandle, CURLOPT_NOBODY  , true);  // we don't need body
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_exec($curlHandle);
$response = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
curl_close($curlHandle); // Don't forget to close the connection
if ($response == "200") {
echo  "1. Example1 Is Running Fine"; 
}
else {
echo "1. Example1 Seems Down!";
}
?>
<?php
$URL  = 'https://www.example2.com';
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $URL);
curl_setopt($curlHandle, CURLOPT_HEADER, true);
curl_setopt($curlHandle, CURLOPT_NOBODY  , true);  // we don't need body
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_exec($curlHandle);
$response = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
curl_close($curlHandle); // Don't forget to close the connection
if ($response == "200") {
echo  "2. Example2 Is Running Fine"; 
}
else {
echo "2. Example2 Seems Down!";
}
?>
<?php
$URL  = 'http://www.example3.com/';
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $URL);
curl_setopt($curlHandle, CURLOPT_HEADER, true);
curl_setopt($curlHandle, CURLOPT_NOBODY  , true);  // we don't need body
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_exec($curlHandle);
$response = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
curl_close($curlHandle); // Don't forget to close the connection
if ($response == "200") {
echo  "3. Example3 Is Running Fine",""; 
}
else {
echo "3. Example3 Seems Down!";
}
?>
<?php
$URL  = 'https://www.example4.com';
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $URL);
curl_setopt($curlHandle, CURLOPT_HEADER, true);
curl_setopt($curlHandle, CURLOPT_NOBODY  , true);  // we don't need body
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_exec($curlHandle);
$response = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
curl_close($curlHandle); // Don't forget to close the connection
if ($response == "200") {
echo  "4. Example4 Is Running Fine",""; 
}
else {
echo "4. Example4 Seems Down!";
}
?>
<?php
$URL  = 'https://www.example5.com/';
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $URL);
curl_setopt($curlHandle, CURLOPT_HEADER, true);
curl_setopt($curlHandle, CURLOPT_NOBODY  , true);  // we don't need body
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_exec($curlHandle);
$response = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
curl_close($curlHandle); // Don't forget to close the connection
if ($response == "200") {
echo  "5. Example5 Is Running Fine",""; 
}
else {
echo "5. Example5 Seems Down!";

}

?>
</body>
</html>

是否有一种简单的方法可以执行此操作,而不是为每个网站传递单独的卷曲规则?

我已经使用了数组,现在似乎更好了,请查看下面的新代码

<?php
$array = array( 'https://www.example1.com', 'https://www.example2.com', 'http://www.example3.com/', 'https://www.example4.com/', 'https://www.troublefixing.com/', 'https://easyfoodhealthtips.com/', 'https://cryptotipstricks.com/', );
foreach( $array as $value ) {
$URL  = $value;

$curlHandle = curl_init();

curl_setopt($curlHandle, CURLOPT_URL, $URL);

curl_setopt($curlHandle, CURLOPT_HEADER, true);

curl_setopt($curlHandle, CURLOPT_NOBODY  , true);  // we don't need body

curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);

curl_exec($curlHandle);

$response = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);

curl_close($curlHandle); // Don't forget to close the connection
if ($response == "200") {
echo "<a href='$value' target='_blank'>$value</a> Is Running Fine <br />",""; 

}



else {
echo "<a href='$value' target='_blank'>$value</a> Seems Down! <br />","";

}
}
?>

在结果页面上显示完整的URL,因为$ value包含完整的URL,我想显示网站的名称,例如Example1,Example2等。

1 个答案:

答案 0 :(得分:-1)

请查看此链接http://php.net/curl_multi_init

&#13;
&#13;
<?php
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();

// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://lxr.php.net/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch2, CURLOPT_HEADER, 0);

//create the multiple cURL handle
$mh = curl_multi_init();

//add the two handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

$active = null;
//execute the handles
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);

?>
&#13;
&#13;
&#13;