好的,我正在编写一个脚本来检查给定的网站是否返回403。我加入了一些零碎的工作,如果我一次只检查一个网站,但它不适用于多个网站,它确实有效。
$url = $_POST['site'];
$many_urls = explode(",", $url);
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);
/* Check for 403 (forbidden). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
foreach ($many_urls as $urls)
{
if($httpCode == 403) {
echo "<h2>$url is <font color='red'>Forbidden.</font><h2>";
} else echo "<h2>$url <font color='green'>Works.</font><h2>";
}
curl_close($handle);
因此,如果我输入例如: google.com,youtube.com,forbiddenwebsite.com
它应该返回:
google.com 作品 youtube.com 作品 forbiddenwebsite.com 禁止(在不同的路线上)
我非常确定 foreach 部分存在问题。
任何帮助将不胜感激。感谢。
答案 0 :(得分:0)
如果您要以列表形式独立检查每个网址,则您需要在循环中进行卷曲。否则,您需要检查&#34; google.com,youtube.com,forbiddenwebsite.com&#34;的标头,这不是有效地址。试试这个:
/* exploding the url list will give an array with 1 item in the case of only 1 url */
$urls = explode(',', $_POST['site']);
/* wrap the whole thing in the loop, check each url individually */
foreach($urls as $url){
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);
/* Check for 403 (forbidden). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 403) {
echo "<h2>$url is <font color='red'>Forbidden.</font><h2>";
} else echo "<h2>$url <font color='green'>Works.</font><h2>";
curl_close($handle);
}
您可能希望为后期变量中传递的空字符串添加某种检查。