How to fetch all links of page using php (SEO TOOLS)

How to fetch all links of page using php (SEO TOOLS)

IF You want to extract links of any web page using php script and store in your database or simply create url extractor to analyse any website then in this tutorial i am going to share simple php function which help you to extract all links of any web page, you only need to pass complete url of that website page which links you want to extract and it’ll return all the internal and external links of given web page in array form.

Here is the PHP function to extract url’s/links of given web page.

functiongetAllLinks($url){

$urlData=file_get_contents($url);

$dom=newDOMDocument();

@$dom->loadHTML($urlData);

$xpath=newDOMXPath($dom);

$hrefs=$xpath->evaluate("/html/body//a");

for($i=0;$i<$hrefs->length;$i++){

$href=$hrefs->item($i);

$url=$href->getAttribute('href');

$url=filter_var($url, FILTER_SANITIZE_URL);

if(!filter_var($url, FILTER_VALIDATE_URL)===false){

$urlList[]=$url;

}

}

returnarray_unique($urlList);

}

Now pass URL of any web page which links you are going to extract and it’ll return all the links of that web page in array form.

$url='http://iamrohit.in/rohit-kumar';

var_dump(getAllLinks($url));


Please Share this course

It Looks like you have completed your course!! We have created Online series based on the above content. You can give a test and check your skill.




Be a Part of CollegeSpike by giving your precious comment here and help us in improving our content.