突然、php ファイルの get で 503 エラーが発生しました。その理由を知っている人はいますか?別の方法があります。
php $URL = "http://www.website.com/foo.html"; 
$a =file_get_contents("$URL"); echo ($a); 
突然、php ファイルの get で 503 エラーが発生しました。その理由を知っている人はいますか?別の方法があります。
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);
}