Skip to content

Instantly share code, notes, and snippets.

@mosleim
Forked from nedevl/backlinkchecker.php
Created December 10, 2017 10:45
Show Gist options
  • Save mosleim/dbe977cc331fb5bc0ff1d84288358ceb to your computer and use it in GitHub Desktop.
Save mosleim/dbe977cc331fb5bc0ff1d84288358ceb to your computer and use it in GitHub Desktop.
A useful function written in PHP to check whether a website has a backlink to yours and if it is a Google Friendly one.
<?php
/*
* $backlink - the link we are looking for
* $url - the website against we are checking the $backlink
*
*/
function backlinkCheck($backlink, $url) {
$backlinkexists = false;
$backlinknofollow = false;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
$dom = new DomDocument();
libxml_use_internal_errors(true);
$dom -> loadHTML($content);
$anchors = $dom -> getElementsByTagName('a');
libxml_clear_errors();
foreach ($anchors as $anchor) {
$link = $anchor -> getAttribute("href");
//check if the link exists among links on the $url
if ($link == $backlink) {
$backlinkexists = true;
$anchorhtml = strtolower($anchor -> C14N());
if (strpos($anchorhtml, 'nofollow') !== false) {
$backlinknofollow = true;
}
break;
}
}
if ($backlinkexists && !$backlinknofollow) {
// backlink exists and it doesn't has a rel attribute with nofollow value
} else if ($backlinkexists && $backlinknofollow) {
// backlink exists and it does has a rel attribute with nofollow value
} else {
// backlink doesn't exists
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment