我正在用file_get_contents从网上下载一个文件。有时我会得到503服务不可用的或404未找到的。
警告: file_get_contents(http://somewhereoverinternets.com)功能.文件-获取内容:未能打开流: HTTP请求失败!第20行somesourcefile.php中不可用的HTTP/1.0 503服务
我如何得到这个错误代码- 503 ? 404,200?为这些案件做好处理。
发布于 2011-11-20 21:16:41
试试卷曲:
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
if(!curl_errno($ch)){
return $data;
}else{
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
}发布于 2013-03-28 15:33:58
实际上,您可以在使用file_get_contents时获取所需的标头。这些标头可以在$http_response_header在全局范围内创建的数组中使用。
例如,以下代码(其中URI指向本地服务器上不存在的资源):
$contents = @file_get_contents('http://example.com/inexistent');
var_dump($http_response_header);给出以下结果:
array(8) {
[0]=>
string(22) "HTTP/1.1 404 Not Found"
[1]=>
string(22) "Cache-Control: private"
[2]=>
string(38) "Content-Type: text/html; charset=utf-8"
[3]=>
string(25) "Server: Microsoft-IIS/7.0"
[4]=>
string(21) "X-Powered-By: ASP.NET"
[5]=>
string(35) "Date: Thu, 28 Mar 2013 15:30:03 GMT"
[6]=>
string(17) "Connection: close"
[7]=>
string(20) "Content-Length: 5430"
}https://stackoverflow.com/questions/8204586
复制相似问题