使用 cURL 远程检查 HTTP 状态

发布于 2024-11-13 08:17:58 字数 1226 浏览 3 评论 0原文

我正在尝试根据下面的代码生成行。有人能告诉我我做错了什么吗?我无法让错误报告在我所在的地方工作。您可以在 while 循环中使用 cURL 吗?

while ($row = mysql_fetch_array($result_entries))

  if ($row['active'] == "y") {

    $ch = curl_init($row['url']);

    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    // $retcode > 400 -> not found, $retcode = 200, found.
    curl_close($ch);

    if ($retcode == '[4-5][0-9][0-9]'){
        echo "<tr class=\"bad"\"><td><a href=\"" . $row['code'] . "\" target=\"_blank\">". $row['code'] . "</a></td><td>" . $row['url'] . "</td><td>" . $row['requester'] . "</td></tr>\n\n";

    } else if ($retcode == '[2-3][0-9][0-9]'){
        echo "<tr class=\"good"\"><td><a href=\"" . $row['code'] . "\" target=\"_blank\">". $row['code'] . "</a></td><td>" . $row['url'] . "</td><td>" . $row['requester'] . "</td></tr>\n\n";

    } else {
        echo "<tr class=\"inactive\"><td>". $row['code'] . "</td><td>" . $row['url'] . "</td><td>" . $row['requester'] . "</td></tr>\n\n";
    }
}

I am trying to generate rows based on the code below. Would someone be able to tell me what I am doing wrong? I can't get the error reporting to work where I am. Are you able to use cURL within while loops?

while ($row = mysql_fetch_array($result_entries))

  if ($row['active'] == "y") {

    $ch = curl_init($row['url']);

    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    // $retcode > 400 -> not found, $retcode = 200, found.
    curl_close($ch);

    if ($retcode == '[4-5][0-9][0-9]'){
        echo "<tr class=\"bad"\"><td><a href=\"" . $row['code'] . "\" target=\"_blank\">". $row['code'] . "</a></td><td>" . $row['url'] . "</td><td>" . $row['requester'] . "</td></tr>\n\n";

    } else if ($retcode == '[2-3][0-9][0-9]'){
        echo "<tr class=\"good"\"><td><a href=\"" . $row['code'] . "\" target=\"_blank\">". $row['code'] . "</a></td><td>" . $row['url'] . "</td><td>" . $row['requester'] . "</td></tr>\n\n";

    } else {
        echo "<tr class=\"inactive\"><td>". $row['code'] . "</td><td>" . $row['url'] . "</td><td>" . $row['requester'] . "</td></tr>\n\n";
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左岸枫 2024-11-20 08:17:58

您应该对 $retcode 变量使用数字比较

if($retcode >= 400 && $retcode <= 599) {
  // Code for 400> status here
} else if ($retcode >= 200 && $retcode <= 399) {
  // Code for 200-300 status Here
} else {
  // Fall through case here
}

您的代码未正确将结果代码与字符串(您试图将其视为正则表达式)进行比较。

数字比较将更快、更容易阅读并且是更安全的解决方案。

You should use numerical comparisons on your $retcode variable

if($retcode >= 400 && $retcode <= 599) {
  // Code for 400> status here
} else if ($retcode >= 200 && $retcode <= 399) {
  // Code for 200-300 status Here
} else {
  // Fall through case here
}

Your code isn't correctly comparing the result code to the string, (which you are trying to treat as a regular expression).

Numerical comparisons will be faster, easier to read, and a more secure solution.

姐不稀罕 2024-11-20 08:17:58

看来您正在尝试在比较中使用正则表达式。 $retcode 只是一个整数。它应该类似于:

if ($retcode == 400){

如果您想要很多,您可以在 $retcode 上使用 preg_match()代码来执行 if 的同一块,尽管最好使用 switch 或大于小于,即 if($retcode >= 400 && $retcode <= 599){

It looks like you are trying to use a regex in the comparison. $retcode will just be an integer. It should just be something like:

if ($retcode == 400){

You can use preg_match() on $retcode if you want many codes to execute the same block of your if, although it may be better to use a switch or greater than less than, i.e. if($retcode >= 400 && $retcode <= 599){.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文