1

我正在构建一个脚本,其目标是检查多达 100 个 URL 的有效性(第 404 号)。

URL 中唯一的变量是页码,如下所示:

http://example.com/category/id/products/page/1
http://example.com/category/id/products/page/2

以此类推,最多 100 个,

一旦我的代码到达无效的 URL,我希望它停止并回显它已达到的数字,这是我试图无济于事的代码:

$url ="http://example.com/category/id/products/page/1";

if (false !== strpos($url, $id)) {

    $pageNumber = 2;
    $check = true;

do{

    $urlIterate = "http://example.com/category/id/products/page/".$pageNumber;

    if(false !== strpos($urlIterate, $id)){

        $pageNumber++;

    }

    else{

        $check = false;

    }

}

while($pageNumber <= 99);

}

else{

    $check = false;
    echo 'No pages were found at all';

}

echo "There were ". $pageNumber." pages.;

?>
4

2 回答 2

1

我不确定这是否是你要找的,但试试这个:

<?php

    $id_to_search = "90";

    for ($i = 1; $i <= 100; $i++) {
        $url = "http://example.com/category/id/products/page/" . $i;
        $values = parse_url($url);
        $paths = explode('/', $values['path']);
        $id_from_url = $paths[5];
        if ($id_to_search === $id_from_url) {
            $headers = get_headers($url);
            if ($headers[0] == 'HTTP/1.0 404 Not Found') {
                echo "URL Found! URL is invalid(404). URLs searched = " . $i . "<br>";
            } else {
                echo "URL is valid<br>";
            }
        } else {
            echo "URL was searched but it does not match the ID we are looking for<br>";
        }
    }
于 2014-10-23T13:25:35.920 回答
0

你为什么不使用for循环?当我们知道我们需要多少迭代时会更好。

for($i = 1; $1<=100; $i++){
    $urlIterate = "http://example.com/category/id/products/page/".$i; //generate url
    $headers = get_headers($urlIterate, 1); //get headers
    if($headers[0] != 'HTTP/1.1 200 OK'){ //if we have an error
        if($i > 1) //if there was at least one found
            echo 'Last found number is ' . ($i-1);
        else
            echo 'No pages were found at all';
        break; //stops the 'for' loop
    }
}

您的代码在 url 中寻找$id- 有什么意义?

于 2014-10-23T12:56:39.353 回答