-2

我正在尝试制作一个脚本,该脚本将加载所需的 URL(由用户输入)并检查该页面是否在其域发布到我的网站之前链接回我的域。我对正则表达式不是很有经验,这就是我到目前为止所拥有的:

$loaded = file_get_contents('http://localhost/small_script/page.php');
// $loaded will be equal to the users site they have submitted
$current_site = 'site2.com';
// $current_site is the domain of my site, this the the URL that must be found in target site
$matches = Array();
$find = preg_match_all('/<a(.*?)href=[\'"](.*?)[\'"](.*?)\b[^>]*>(.*?)<\/a>/i', $loaded, $matches);

$c = count($matches[0]);
$z = 0;
while($z<$c){
  $full_link = $matches[0][$z];
  $href = $matches[2][$z];
  $z++;

  $check = strpos($href,$current_site);
    if($check === false) {

    }else{
    // The link cannot have the "no follow" tag, this is to check if it does and if so, return a specific error
    $pos = strpos($full_link,'no follow');

    if($pos === false) {
     echo $href;
    }
      else {
    //echo "rel=no follow FOUND";
    }

    }

}

如您所见,它非常混乱,我完全确定它的发展方向。我希望有人能给我一个小巧、快速、简洁的脚本,它可以完全按照我的尝试进行。

  1. 加载用户输入的指定 URL
  2. 检查指定的 URL 是否链接回我的网站(如果没有,返回错误代码 #1)
  3. 如果链接存在,请检查“不关注”,如果找到则返回错误代码 #2
  4. 如果一切正常,请将变量设置为 true,这样我就可以继续使用其他功能(例如在我的页面上显示它们的链接)
4

1 回答 1

0

这是代码:) http://www.merchantos.com/makebeta/php/scraping-links-with-php/帮助

<?php

$my_url = 'http://online.bulsam.net';
$target_url = 'http://www.bulsam.net';
$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';

// make the cURL request to $target_url
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html= curl_exec($ch);
if (!$html) {
    echo "<br />cURL error number:" .curl_errno($ch);
    echo "<br />cURL error:" . curl_error($ch);
    exit;
}

// parse the html into a DOMDocument
$dom = new DOMDocument();
@$dom->loadHTML($html);


// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");

// find result
$result = is_my_link_there($hrefs, $my_url);

if ($result == 1) {

    echo 'There is no link!!!';
} elseif ($result == 2) {

    echo 'There is, but it is NO FOLLOW !!!';
} else {

    // blah blah blah
}

// used functions

function is_my_link_there($hrefs, $my_url) {

    for ($i = 0; $i < $hrefs->length; $i++) {

        $href = $hrefs->item($i);

        $url = $href->getAttribute('href');

        if ($my_url == $url) {

            $rel = $href->getAttribute('rel');

            if ($rel == 'nofollow') {

                return 2;
            }

            return 3;
        } 
    }

    return 1;
}
于 2010-09-24T01:29:30.093 回答