开发者

Remotely checking HTTP Status with cURL

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";
    }
}


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.


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){.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜