How to get all links from a site using PHP

This post will help you to get all links from a site using PHP

Having a complete list of your web pages in your site is most important in the current search landscape. There is no need to use a tool to get all the links from a site. Use the following PHP code to get all the links.

$html = file_get_contents('');
$dom = new DOMDocument();
// grab all the links on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
echo $url.'<br />';

Kayalvizhi Ganesan

Web developer, Blogger, You-tuber and a learner. I am always open up for learning new things and sharing it with people. This Blog is to share the things which I have learnt. My goal is to help TYRO to become a PRO

Leave a Reply

Your email address will not be published. Required fields are marked *