另一个网站上的Php反向链接检查器
我正在尝试制作一个脚本,以检查网页是否有指向我的网页的反向链接。我已经找到了这个脚本,但问题是即使有反向链接,它也会返回错误消息“找不到反向链接”。有人能告诉我这个脚本有什么问题吗? 以下是我正在使用的脚本:另一个网站上的Php反向链接检查器,php,simple-html-dom,Php,Simple Html Dom,我正在尝试制作一个脚本,以检查网页是否有指向我的网页的反向链接。我已经找到了这个脚本,但问题是即使有反向链接,它也会返回错误消息“找不到反向链接”。有人能告诉我这个脚本有什么问题吗? 以下是我正在使用的脚本: require('simple_html_dom.php'); function CheckReciprocal( $targetUrl, $checkLinkUrl, $checkNofollow = true ) { $html = file_get_html($ta
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'; }
谢谢大家! 我不知道这个简单的\u html\u dom.php的$html->find()是如何工作的,因为从来没有使用过它,但似乎你的问题就在那里。我相信好的ol'DOMDocument+regex 只需编写一个函数并对其进行测试,只需在$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";
}
如果您不喜欢它并且仍然想使用简单的html dom,只需确保find()是如何工作的,因为如果它只匹配可能会很麻烦的精确值。显示此页面中的
元素。@Philippe K感谢您的更正:)@kasyx这是测试html页面锚上的a标记,它带有http://但它不会在注释中显示您的代码为$targetUrl
实例化一个值,但会检查$test
。哪个变量包含要扫描的URL?但此链接中可能有www
,以此类推。我认为,问题是,regexp无法计算此链接。您正在寻找http://mysite.com
并且他可以链接到http://www.mysite.com