2012-09-06 2 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; 
} 

여기에서 시도 URL을 아래 참조 : http://www.php.net/manual/en/function.file-exists.php#75064

... 오른쪽 위의 포스트 아래, 컬 해결 방법이 있습니다 :

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

업데이트 코드 : -

은 아래 URL을 참조 당신은 태그 찾기 ID 또는 클래스 SimpleHtmlDom 클래스를 사용할 수 있습니다

http://simplehtmldom.sourceforge.net/

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

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

http://davidwalsh.name/php-notifications

+0

무엇을 연결 시간 초과 (- 코드), 403 및 301에 대해? 더 나은 방법은 HTTP 응답 코드를 확인하는 것이 200이라고 생각합니까? 허위 사실. –

+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