2011-06-06 113 views
-2

我想根據下面的代碼生成行。有人能告訴我我做錯了什麼嗎?我無法讓錯誤報告在我所在的地方工作。你能夠在while循環中使用cURL嗎?用cURL遠程檢查HTTP狀態

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

'$ retcode =='[4-5] [0-9] [0-9]'是什麼意思? – 2011-06-06 13:58:23

+0

請添加更多信息,以何種方式無法按預期工作?此外,'$ retcode'的數值比較會更好,在'if-elsif-else'塊後面調用'curl_close()'可能是理智的,但這肯定不是你錯誤的原因。 – 2011-06-06 13:58:58

+0

我一直收到500錯誤,因爲它們沒有在這裏打開錯誤報告。所以我不太確定,我也是一個PHP新手。它應該是着色行工作,破碎和無效。 – Bee 2011-06-06 14:51:31

回答

3

你應該在你的$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 
} 

代碼中使用的數值進行比較時,沒有結果的代碼比較正確的字符串,(你正試圖把一個正則表達式)。

數字比較將更快,更易於閱讀和更安全的解決方案。

+0

謝謝我改變了它。不幸的是,它仍然不適合我。 – Bee 2011-06-06 14:52:11

0

它看起來像你試圖在比較中使用正則表達式。 $retcode只是一個整數。它應該只是是這樣的:

if ($retcode == 400){

您可以在$retcode使用preg_match()如果你想許多代碼執行的同一塊你如果,儘管它可能是更好的使用switch或大於小於,即if($retcode >= 400 && $retcode <= 599){

+0

請查看我的回答,他應該檢查'$ retcode'是否在一個範圍內,以複製他的非工作正則表達式的預期行爲。 – 2011-06-06 14:38:14

+0

@Beaks你的答案更快,可能更乾淨,但我不明白downvote ...我的三個建議中的最後一個看起來是完全一樣的代碼,一個開關可以處理多個特定的代碼,一個正則表達式可以以任何方式過濾代碼。對不起,我是新來的(回答)。我仍然在學習蒼蠅什麼的。 – panda 2011-06-06 21:29:24