Skip to content

Instantly share code, notes, and snippets.

@nedevl
Last active October 1, 2020 14:40
Embed
What would you like to do?
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
}
}
?>
@nedevl
Copy link
Author

nedevl commented Feb 22, 2017

If you find it useful, use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment