Bart S.
Bart S.

Reputation: 23

PHP multiple file_get_contents on data of previous file_get_contents

I found this code to check for links on an URL.

<?php
$url = "http://example.com";
$input = @file_get_contents($url);
$dom = new DOMDocument();
$dom->strictErrorChecking = false;
@$dom->loadHTML($input);
$links = $dom->getElementsByTagName('a');
foreach($links as $link) {
   if ($link->hasAttribute('href')) {
      $href = $link->getAttribute('href');
      if (stripos($href, 'shows') !== false) {
         echo "<p>http://example.com" . $href . "</p>\n";
      }
   }
}

?>

Works good, it shows all the links that contains 'shows'. For example the script above find 3 links, so i get:

<p>http://example.com/shows/Link1</p>
<p>http://example.com/shows/Link2</p>
<p>http://example.com/shows/Link3</p>

Now the thing i try to do is to check those urls i just fetched also for links that contains 'shows'.

To be honest i'm a php noob, so i don't know where to start :(

Regards, Bart

Upvotes: 0

Views: 1624

Answers (1)

Michal
Michal

Reputation: 3368

Something like:

function checklinks($url){
$input = @file_get_contents($url);
$dom = new DOMDocument();
$dom->strictErrorChecking = false;
@$dom->loadHTML($input);
$links = $dom->getElementsByTagName('a');
foreach($links as $link) {
   if ($link->hasAttribute('href')) {
      $href = $link->getAttribute('href');
      if (stripos($href, 'shows') !== false) {
         echo "<p>" . $url . "/" . $href . "</p>\n";
         checklinks($url . "/" . $href);
      }
   }
}
}

$url = "http://example.com";
checklinks($url);

Make it recursive - call the function again in the function itself.

Upvotes: 2

Related Questions