使用 cURL 远程检查 HTTP 状态
我正在尝试根据下面的代码生成行。有人能告诉我我做错了什么吗?我无法让错误报告在我所在的地方工作。您可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该对
$retcode
变量使用数字比较您的代码未正确将结果代码与字符串(您试图将其视为正则表达式)进行比较。
数字比较将更快、更容易阅读并且是更安全的解决方案。
You should use numerical comparisons on your
$retcode
variableYour 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.
看来您正在尝试在比较中使用正则表达式。
$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 aswitch
or greater than less than, i.e.if($retcode >= 400 && $retcode <= 599){
.