Внезапно мой файл php get дает мне ошибку 503. Может кто-нибудь знает, почему и есть ли еще один способ.Php File get stop working
php $URL = "http://www.website.com/foo.html";
$a =file_get_contents("$URL"); echo ($a);
Внезапно мой файл php get дает мне ошибку 503. Может кто-нибудь знает, почему и есть ли еще один способ.Php File get stop working
php $URL = "http://www.website.com/foo.html";
$a =file_get_contents("$URL"); echo ($a);
Sol 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";
}
Sol 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
}
Sol 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-код 503 - это код для «Временно недоступный сервис». Возможно, проверьте его с помощью функции get_headers($url);
Размер файла? –