I am building a script thats goal is to check up to 100 URLS for validity (No 404).
The only variable in the URL is the page number, like so:
http://example.com/category/id/products/page/1
http://example.com/category/id/products/page/2
and so on up to 100,
as soon as my code reaches an invalid URL, I want it to stop and echo the number it has reached, this is the code I am trying to no avail:
$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.;
?>
Im not sure if this is what youre looking for, but try this:
<?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>";
}
}