Php文件停止工作
突然,我的php文件get给了我一个503错误。有人知道原因吗?还有其他方法吗Php文件停止工作,php,Php,突然,我的php文件get给了我一个503错误。有人知道原因吗?还有其他方法吗 php $URL = "http://www.website.com/foo.html"; $a =file_get_contents("$URL"); echo ($a); Sol 1: Sol 2: 使用PHP中的此类命令,您可以在它们前面加上@以抑制此类警告 @file_get_contents('http://somenotrealurl.com/notrealpage'); file\u get\u
php $URL = "http://www.website.com/foo.html";
$a =file_get_contents("$URL"); echo ($a);
Sol 1:
Sol 2:
使用PHP中的此类命令,您可以在它们前面加上@以抑制此类警告
@file_get_contents('http://somenotrealurl.com/notrealpage');
file\u get\u contents()
如果出现故障,则返回FALSE,因此如果您对照返回的结果检查,则可以处理故障
$pageDocument = @file_get_contents('http://somenotrealurl.com/notrealpage');
if ($pageDocument === false) {
// Handle error
}
Sol 3:
虽然file_get_内容非常简洁方便,但为了更好地控制,我倾向于使用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);
}
你确定你有一个正确的URL吗?HTTP代码503是“服务暂时不可用”的代码。
也许可以使用函数
get_headers($url)进行检查代码>如果答案为已接受,则可以将其标记为已接受。
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);
}