Get Link from a URL using PHP

3236475535_58a6fb66a4_URL-PHP

if you want to fetch all link from a particular URL using PHP. Then you can use following code.

First of all create a php file for submitting  URL lets name it as getUrl.php

<form action="geturl.php" method="post">
Enter URL <input type="text" name="siteurl" value="">
<p style="color:#FF0000;"> [ex. - http://facebook.com]</p>
<input type="submit" name="url_list">
</form>

You can write code for fetching LINKS on geturl.php

<?php
if(isset($_POST['url_list']))
{
$srcurl=$_POST['url_list'];
$HTML = file_get_contents($srcurl);
$dom = new DOMDocument();
@$dom->loadHTML($HTML);
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
for ($i = 0; $i length; $i++)
{
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
if($url!='#')
{
echo '<a href=".$srcUrl." ".$url."="">'.$url.'</a><br>';
}
}
}
?>

you can do many other experiments like
you can save it to database.
you can fetch multiple url.
you can do it using cron job.

Photo by schoschie

This entry was posted in Development, PHP. Bookmark the permalink.

Leave a Reply

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

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.