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

3 thoughts on “How to get all links from a site using PHP

  • March 14, 2019 at 7:16 am

    Thanks for your recommendations on this blog. A single thing I wish to say is that often purchasing gadgets items on the Internet is nothing new. Actually, in the past decades alone, the market for online gadgets has grown considerably. Today, you could find practically any kind of electronic gizmo and tools on the Internet, including cameras as well as camcorders to computer elements and games consoles.

  • May 20, 2019 at 11:12 am

    This discusses some good ideas-but I think you are missing something. I hope to see you expand this, because you are a very eloquent writer and I get immense value from reading your posts.


Leave a Reply

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