2012-09-06 59 views

回答

2

见下面链接

How can I check if a URL exists via PHP?

或尝试

$file = 'http://www.domain.com/somefile.jpg'; 
$file_headers = @get_headers($file); 
if($file_headers[0] == 'HTTP/1.1 404 Not Found') { 
    $exists = false; 
} 
else { 
    $exists = true; 
} 

从这里:http://www.php.net/manual/en/function.file-exists.php#75064

... ...而低于上述帖子,有一个卷曲的解决方案:

function url_exists($url) { 
    if (!$fp = curl_init($url)) return false; 
    return true; 
} 

更新代码: -

可以在标签使用SimpleHtmlDom班找到ID或类

看到下面的网址

http://simplehtmldom.sourceforge.net/

http://simplehtmldom.sourceforge.net/manual_api.htm

http://sourceforge.net/projects/simplehtmldom/files/

http://davidwalsh.name/php-notifications

+0

什么关于连接超时( - 代码),403s和301s? 我认为更好的办法是检查http响应代码是200吗?真假。 –

+0

它不是看是否存在一个url,它是看看中的HREF是否等于“Example.com” – alexander7567

+0

我有更新我的答案请看看。 –

0

这是我发现的以防其他人需要它也!

$url = "http://example.com/test.html"; 
    $searchFor = "example.com" 
    $input = @file_get_contents($url) or die("Could not access file: $url"); 
    $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>"; 
    if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) { 
    foreach($matches as $match) { 
     echo $match[2]; 
     if ($match[2] == $searchFor) 
     { 
      $isMatch = 1; 
     } else { 
      $isMatch= 0; 
     } 
     // $match[0] = A tag 
     // $match[2] = link address 
     // $match[3] = link text 
    } 
    } 

     if ($isMatch) 
     { 
      echo "<p><font color=red size=5 align=center>The page specified does contain your link. You have been credited the award amount!</font></p>"; 
     } else { 
      echo "<p><font color=red size=5 align=center>The specified page does not have your referral link.</font></p>"; 
     } 
+0

[小马他来....](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Tchoupi