sam*_*ami 69 php file-get-contents
我正在使用file_get_contents()来访问URL.
file_get_contents('http://somenotrealurl.com/notrealpage');
Run Code Online (Sandbox Code Playgroud)
如果URL不是真实的,则返回此错误消息.如何才能优雅地将其置于错误状态,以便我知道页面不存在并相应地执行操作而不显示此错误消息?
file_get_contents('http://somenotrealurl.com/notrealpage')
[function.file-get-contents]:
failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
in myphppage.php on line 3
Run Code Online (Sandbox Code Playgroud)
例如在zend你可以说: if ($request->isSuccessful())
$client = New Zend_Http_Client();
$client->setUri('http://someurl.com/somepage');
$request = $client->request();
if ($request->isSuccessful()) {
//do stuff with the result
}
Run Code Online (Sandbox Code Playgroud)
ynh*_*ynh 106
您需要检查HTTP响应代码:
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') != "200"){
echo "error";
}else{
file_get_contents('http://somenotrealurl.com/notrealpage');
}
Run Code Online (Sandbox Code Playgroud)
Orb*_*ing 60
使用PHP中的这些命令,您可以在它们前面添加一个@来抑制此类警告.
@file_get_contents('http://somenotrealurl.com/notrealpage');
Run Code Online (Sandbox Code Playgroud)
FALSE如果发生故障,file_get_contents()会返回,因此如果您检查返回的结果,那么您可以处理失败
$pageDocument = @file_get_contents('http://somenotrealurl.com/notrealpage');
if ($pageDocument === false) {
// Handle error
}
Run Code Online (Sandbox Code Playgroud)
Grz*_*orz 25
每次file_get_contents使用http包装器调用时,都会创建本地范围内的变量:$ http_response_header
此变量包含所有HTTP标头.get_headers()由于只执行一个请求,因此该方法优于函数.
注意:2个不同的请求可以以不同方式结 例如,get_headers()将返回503并且file_get_contents()将返回200.并且您将获得正确的输出但由于get_headers()调用中的503错误而不会使用它.
function getUrl($url) {
$content = file_get_contents($url);
// you can add some code to extract/parse response number from first header.
// For example from "HTTP/1.1 200 OK" string.
return array(
'headers' => $http_response_header,
'content' => $content
);
}
// Handle 40x and 50x errors
$response = getUrl("http://example.com/secret-message");
if ($response['content'] === FALSE)
echo $response['headers'][0]; // HTTP/1.1 401 Unauthorized
else
echo $response['content'];
Run Code Online (Sandbox Code Playgroud)
这个方法也可以让你跟踪存储在不同变量中的几个请求头,因为如果你使用file_get_contents()$ http_response_header会被覆盖在本地范围内.
nik*_*org 16
虽然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);
}
Run Code Online (Sandbox Code Playgroud)
您可以将 'ignore_errors' => true 添加到选项中:
$options = [
'http' => [
'ignore_errors' => true,
'header' => "Content-Type: application/json\r\n",
],
];
$context = stream_context_create($options);
$result = file_get_contents('http://example.com', false, $context);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您将能够从服务器读取响应。
简单实用(易于在任何地方使用):
function file_contents_exist($url, $response_code = 200)
{
$headers = get_headers($url);
if (substr($headers[0], 9, 3) == $response_code)
{
return TRUE;
}
else
{
return FALSE;
}
}
Run Code Online (Sandbox Code Playgroud)
例:
$file_path = 'http://www.google.com';
if(file_contents_exist($file_path))
{
$file = file_get_contents($file_path);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
为了避免重复请求作为评论说Orbling上的答案YNH你可以结合自己的答案。如果您首先得到有效响应,请使用它。如果没有找出问题所在(如果需要)。
$urlToGet = 'http://somenotrealurl.com/notrealpage';
$pageDocument = @file_get_contents($urlToGet);
if ($pageDocument === false) {
$headers = get_headers($urlToGet);
$responseCode = substr($headers[0], 9, 3);
// Handle errors based on response code
if ($responseCode == '404') {
//do something, page is missing
}
// Etc.
} else {
// Use $pageDocument, echo or whatever you are doing
}
Run Code Online (Sandbox Code Playgroud)