0

我正在尝试制作一个脚本来检查网页是否有指向我的页面的反向链接。我找到了这个脚本,但问题是即使有反向链接,它也会返回错误消息“未找到反向链接”。有人可以告诉我这个脚本有什么问题吗?这是我正在使用的脚本:

    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'; }

谢谢!

4

1 回答 1

0

我不知道 simple_html_dom.php 的 $html->find() 是如何工作的,因为从未使用过它,但似乎你的问题就在那里。我会相信好的 ol' 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() 是如何工作的,因为如果它只匹配可能很麻烦的精确值。

于 2013-04-30T15:20:03.243 回答