file_get_contents和错误代码 [英] file_get_contents and error codes

查看:121
本文介绍了file_get_contents和错误代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 file_get_contents 从网上下载文件。
有时会收到 503服务不可用 404未找到 b
$ b

I'm downloading a file from the web with file_get_contents. Sometimes I get 503 Service Unavailable or 404 Not Found.


警告:file_get_contents(http://somewhereoverinternets.com)
[function.file-get-contents]:无法打开流:HTTP请求
失败! HTTP / 1.0 503服务不可用somesourcefile.php on
20 $ / $>

Warning: file_get_contents(http://somewhereoverinternets.com) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable in somesourcefile.php on line 20

如何得到这个错误代码 - 503? 404,200?
为这些情况制定过程。

How can I get this error code - 503 ? 404, 200? To make the process for these cases.

推荐答案

尝试使用curl代替:

Try curl instead:

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);
}

这篇关于file_get_contents和错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆