2014-06-14 4 views
2

내 사이트에 파일을 다운로드하기위한 몇 가지 링크가 있지만 다운로드 링크가 온라인 상태인지 확인하는 PHP 스크립트를 만들고 싶습니다.PHP는 파일을 다운로드하지 않고 링크를 다운로드

$cl = curl_init($url); 
curl_setopt($cl,CURLOPT_CONNECTTIMEOUT,10); 
curl_setopt($cl,CURLOPT_HEADER,true); 
curl_setopt($cl,CURLOPT_NOBODY,true); 
curl_setopt($cl,CURLOPT_RETURNTRANSFER,true); 

if(!curl_exec($cl)){ 
    echo 'The download link is offline'; 
    die(); 
} 

$code = curl_getinfo($cl, CURLINFO_HTTP_CODE); 
if($code != 200){ 
    echo 'The download link is offline'; 
}else{ 
    echo 'The download link is online!'; 
} 

문제는 그것이 정말 느리게 만드는 전체 파일을 다운로드하는 것입니다, 나는 단지 헤더를 확인해야합니다 이것은 내가 사용하고 코드입니다. 저 컬이 CURLOPT_CONNECT_ONLY 옵션을 가지고있는 것을 보았습니다. 그러나 사용하고있는 웹 호스트에는 그 옵션이없는 PHP 버전 5.4가 있습니다. 내가 할 수있는 다른 방법이 있습니까?

+2

이를이 방법의 장점은 당신의 행동이 헤더에 추가하여 URL을 가져 오기 위해 취해 졌는지에 좀 더 많은 정보를 제공하다 이게 도움이 되니? http://stackoverflow.com/questions/2280394/how-can-i-check-if-a-url-exists-via-php – Dan

+0

고맙습니다. get_headers() 함수가 그것을했습니다 :) – Wies

답변

3

CURLOPT_CONNECT_ONLY은 좋지만 PHP 5.5에서만 사용할 수 있습니다. & 거주지. 대신 get_headers을 사용해보세요. 또는 fopen, stream_context_create & stream_get_meta_data을 사용하는 다른 방법을 사용하십시오. 먼저 get_headers 방법

The download link is online! 

Array 
(
    [0] => HTTP/1.0 200 OK 
    [1] => Date: Sat, 14 Jun 2014 15:56:28 GMT 
    [2] => Expires: -1 
    [3] => Cache-Control: private, max-age=0 
    [4] => Content-Type: text/html; charset=ISO-8859-1 
    [5] => Set-Cookie: PREF=ID=6e3e1a0d528b0941:FF=0:TM=1402761388:LM=1402761388:S=4YKP2U9qC6aMgxpo; expires=Mon, 13-Jun-2016 15:56:28 GMT; path=/; domain=.google.com 
    [6] => Set-Cookie: NID=67=Wun72OJYmuA_TQO95WXtbFOK5g-xU53PQZ7dAIBtzCaBWxhXzduHQZfBVPf4LpaK3MVH8ZKbrBIc3-vTKuMlEnMdpWH0mcft5pA_0kCoe4qolDmednpPJqezZF_HyfXD; expires=Sun, 14-Dec-2014 15:56:28 GMT; path=/; domain=.google.com; HttpOnly 
    [7] => P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info." 
    [8] => Server: gws 
    [9] => X-XSS-Protection: 1; mode=block 
    [10] => X-Frame-Options: SAMEORIGIN 
    [11] => Alternate-Protocol: 443:quic 
) 

Array 
(
    [0] => 200 
) 

및 여기 fopen, stream_context_create & stream_get_meta_data을 사용하는 다른 방법이다 :

// Set a test URL. 
$url = "https://www.google.com/"; 

// Get the headers. 
$headers = get_headers($url); 

// Check if the headers are empty. 
if(empty($headers)){ 
    echo 'The download link is offline'; 
    die(); 
} 

// Use a regex to see if the response code is 200. 
preg_match('/\b200\b/', $headers[0], $matches); 

// Act on whether the matches are empty or not. 
if(empty($matches)){ 
    echo 'The download link is offline'; 
} 
else{ 
    echo 'The download link is online!'; 
} 

// Dump the array of headers for debugging. 
echo '<pre>'; 
print_r($headers); 
echo '</pre>'; 

// Dump the array of matches for debugging. 
echo '<pre>'; 
print_r($matches); 
echo '</pre>'; 

그리고이 비롯한 사용되는 덤프 출력 디버깅-될 것이다. 그 출력 여기

// Set a test URL. 
$url = "https://www.google.com/"; 

// Set the stream_context_create options. 
$opts = array(
    'http' => array(
    'method' => 'HEAD' 
    ) 
); 

// Create context stream with stream_context_create. 
$context = stream_context_create($opts); 

// Use fopen with rb (read binary) set and the context set above. 
$handle = fopen($url, 'rb', false, $context); 

// Get the headers with stream_get_meta_data. 
$headers = stream_get_meta_data($handle); 

// Close the fopen handle. 
fclose($handle); 

// Use a regex to see if the response code is 200. 
preg_match('/\b200\b/', $headers['wrapper_data'][0], $matches); 

// Act on whether the matches are empty or not. 
if(empty($matches)){ 
    echo 'The download link is offline'; 
} 
else{ 
    echo 'The download link is online!'; 
} 

// Dump the array of headers for debugging. 
echo '<pre>'; 
print_r($headers); 
echo '</pre>'; 

그리고있다 :

The download link is online! 

Array 
(
    [wrapper_data] => Array 
     (
      [0] => HTTP/1.0 200 OK 
      [1] => Date: Sat, 14 Jun 2014 16:14:58 GMT 
      [2] => Expires: -1 
      [3] => Cache-Control: private, max-age=0 
      [4] => Content-Type: text/html; charset=ISO-8859-1 
      [5] => Set-Cookie: PREF=ID=32f21aea66dcfd5c:FF=0:TM=1402762498:LM=1402762498:S=NVP-y-kW9DktZPAG; expires=Mon, 13-Jun-2016 16:14:58 GMT; path=/; domain=.google.com 
      [6] => Set-Cookie: NID=67=mO_Ihg4TgCTizpySHRPnxuTp514Hou5STn2UBdjvkzMn4GPZ4e9GHhqyIbwap8XuB8SuhjpaY9ZkVinO4vVOmnk_esKKTDBreIZ1sTCsz2yusNLKA9ht56gRO4uq3B9I; expires=Sun, 14-Dec-2014 16:14:58 GMT; path=/; domain=.google.com; HttpOnly 
      [7] => P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info." 
      [8] => Server: gws 
      [9] => X-XSS-Protection: 1; mode=block 
      [10] => X-Frame-Options: SAMEORIGIN 
      [11] => Alternate-Protocol: 443:quic 
     ) 

    [wrapper_type] => http 
    [stream_type] => tcp_socket/ssl 
    [mode] => rb 
    [unread_bytes] => 0 
    [seekable] => 
    [uri] => https://www.google.com/ 
    [timed_out] => 
    [blocked] => 1 
    [eof] => 
) 
+1

Works, thanks a lot ! – Wies

+0

@Wies 여러분을 환영합니다! 나는 또한'fopen','stream_context_create' 및'stream_get_meta_data'를 사용하여 데이터를 검색하는 또 다른 접근법을 추가했습니다. 하나의 방법이 다른 것보다 더 좋거나 더 빠른지 알아보기 위해 탐험 해 볼 가치가 있습니다. – JakeGould

1

curl_setopt($cl, CURLOPT_CUSTOMREQUEST, 'HEAD');을 추가하여 HEAD 요청을 보내주십시오.

관련 문제