Search code examples
phpurldomhref

retrieve href attributes from url with php


i want to retrieve the href attributes within all the anchor tags on the homepage url provided by the website1, crawl the website by one level depth, and retrieve the href attributes within all the anchor tags found on the crawled page, but it doesn't show anything. the function i used is findAndCompare.

<html>
<body>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
website: <input type="text" name="website1"><br>
website: <input type="text" name="website2"><br>
<input type="submit" name="submit">
</form>

</body>
</html> 

<?php
  if(isset($_POST['submit']))
  {
    // form has been submitted
    $form_data = $_POST['website1'];
    findAndCompare($form_data);

  }
  else
  {}

function findAndCompare($url){

// Create a DOM parser object
$dom = new DOMDocument();

$dom->loadHTML($url);

// Iterate over all the <a> tags
foreach($dom->getElementsByTagName('a') as $link) {
        // Show the <a href>
        echo $link->getAttribute('href');
        echo "<br />";
}
}

?>

Solution

  • The method loadHTML() requires HTML source, not an URL. You could load the source with loadHTMLFile() instead:

    function findAndCompare($url){
    
        // Create a DOM parser object
        $dom = new DOMDocument();
    
        // load HTML from URL
        $dom->loadHTMLFile($url);
    
        // Iterate over all the <a> tags
        foreach($dom->getElementsByTagName('a') as $link) {
            // Show the <a href>
            echo $link->getAttribute('href');
            echo "<br />";
        }
    }