갑자기 내 PHP 파일이 내게 503 오류가 발생했습니다. 누구도 그 이유를 알고 또 다른 방법이있을 수 있습니까?PHP 파일이 작동을 멈추게
php $URL = "http://www.website.com/foo.html";
$a =file_get_contents("$URL"); echo ($a);
갑자기 내 PHP 파일이 내게 503 오류가 발생했습니다. 누구도 그 이유를 알고 또 다른 방법이있을 수 있습니까?PHP 파일이 작동을 멈추게
php $URL = "http://www.website.com/foo.html";
$a =file_get_contents("$URL"); echo ($a);
솔 1 :
function get_http_response_code($url) {
$headers = get_headers($url);
return substr($headers[0], 9, 3);
}
if(get_http_response_code('http://somenotrealurl.com/notrealpage') != "404"){
file_get_contents('http://somenotrealurl.com/notrealpage');
}else{
echo "error";
}
솔 2 : PHP에서 같은 명령으로
, 당신은 경고를 억제하기 위해 @로 접두사 수 있습니다.
@file_get_contents('http://somenotrealurl.com/notrealpage');
file_get_contents()
반환 오류가 발생하면 FALSE, 그래서 당신이에 대한 반환 된 결과를 확인하면 다음 오류
$pageDocument = @file_get_contents('http://somenotrealurl.com/notrealpage');
if ($pageDocument === false) {
// Handle error
}
을 처리 할 수
솔 3 :
file_get_contents 동안 매우 간결하고 편리합니다. 더 나은 제어를 위해 Curl 라이브러리를 선호하는 경향이 있습니다. 여기에 예제가 있습니다.
function fetchUrl($uri) {
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $uri);
curl_setopt($handle, CURLOPT_POST, false);
curl_setopt($handle, CURLOPT_BINARYTRANSFER, false);
curl_setopt($handle, CURLOPT_HEADER, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 10);
$response = curl_exec($handle);
$hlength = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
$body = substr($response, $hlength);
// If HTTP response is not 200, throw exception
if ($httpCode != 200) {
throw new Exception($httpCode);
}
return $body;
}
$url = 'http://some.host.com/path/to/doc';
try {
$response = fetchUrl($url);
} catch (Exception $e) {
error_log('Fetch URL failed: ' . $e->getMessage() . ' for ' . $url);
}
도움을 주셔서 감사합니다. – meandme
경우 대답을 승인 된 것으로 표시 할 수 있습니다. –
확실한가요? 올바른 URL이 있습니까? HTTP-Code 503은 "Service Temporarily Unavailable"코드입니다. 아마도이 기능을 통해 확인하십시오 get_headers($url);
파일의 크기는 어느 정도입니까? –