Last active
October 1, 2020 14:40
-
-
Save nedevl/f6fb87ab53f3c765c8eb5e09b16817fe 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
If you find it useful, use it.