我正在尝试制作一个脚本来检查网页是否有指向我的页面的反向链接。我找到了这个脚本,但问题是即使存在反向链接,它也会返回错误消息“未找到反向链接”。有人能告诉我这个脚本有什么问题吗? 这是我正在使用的脚本:
require('simple_html_dom.php');
function CheckReciprocal( $targetUrl, $checkLinkUrl, $checkNofollow = true )
{
$html = file_get_html($targetUrl);
if (empty($html))
{
//@ Could not load file
return false;
}
$link = $html->find('a[href^='.$checkLinkUrl.']',0);
if (empty($link))
{
//@ Link not found
return false;
}
if ( $checkNofollow && $link->hasAttribute('rel') )
{
$attr = $link->getAttribute('rel');
return (preg_match("/\bnofollow\b/is", $attr) ? false : true);
}
return true;
}
$targetUrl = 'http://example.com/test.html';
$checkLinkUrl = 'http://mysite.com';
if ( CheckReciprocal($test, $checkLinkUrl) )
{
echo 'Link found';
}
else { echo 'Link not found or marked as nofollow'; }
谢谢!
我不知道 simple_html_dom.php 的 $html->find() 是如何工作的,因为从未使用过它,但似乎你的问题就在那里。我会相信优秀的 DOMDocument + 正则表达式。
刚刚编写了一个函数并对其进行了测试,只需在 $url 上使用普通域+任何你想要的内容,不用担心 http(s) 或 www 之类的东西:
function checkBackLink($link, $url, $checkNoFollow = true){
$dom = new DOMDocument();
$dom->loadHTMLFile($link);
foreach($dom->getElementsByTagName('a') as $item){
if($checkNoFollow){
if(preg_match('/nofollow/is', $item->getAttribute('rel'))) continue;
}
if($item->hasAttribute('href') === false) continue;
if(preg_match("#^(https?\://)?(www\.)?$url.*#i", $item->getAttribute('href'))) return true;
}
}
if(checkBacklink('the link', 'example.com')){
echo "link found";
} else {
echo "Link not found or marked as nofollow";
}
如果你不喜欢它,但仍然想使用 simple_html_dom,只需确保 find() 是如何工作的,因为如果它只匹配精确的值,可能会很麻烦。
批量锚文本和Dofollow / Nofollow检查器
它允许您根据以下条件集体检查反向链接:
用途: