【问题标题】:Manupulate Error file_get_contents处理错误 file_get_contents
【发布时间】:2019-02-01 16:27:46
【问题描述】:

我有一个脚本

$data = file_get_contents('http://my_example_link/data.xml');

如果http://my_example_link/data.xml 显示错误

打开流失败:HTTP 请求失败! HTTP/1.1 404 未找到

如果上面的链接显示错误,我想创建条件,我将使用引导面板显示警报通知。

我可以那样做吗?

【问题讨论】:

  • 检查文件是否存在并显示错误信息
  • 你的错误是不言自明的;网址不存在。您可以使用 curl 或 get_headers 来检查 url 是否存在,如果不存在则提供错误。上面的 url @Sinto 提供了其中的逻辑

标签: codeigniter if-statement file-get-contents


【解决方案1】:

你可以用curl做,请参考curl_getinfo

<?php
// Create a cURL handle
$ch = curl_init('http://www.example.com/');

// Execute
curl_exec($ch);

// Check HTTP status code
if (!curl_errno($ch)) {
  switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
    case 200:  # OK
      break;
    default:
      echo 'Unexpected HTTP code: ', $http_code, "\n";
  }
}

// Close handle
curl_close($ch);
?>

【讨论】:

    猜你喜欢
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 2011-02-27
    • 2017-10-18
    • 1970-01-01
    相关资源
    最近更新 更多