【问题标题】:Why is relative URL faster in PHP file_get_contents?为什么 PHP file_get_contents 中的相对 URL 更快?
【发布时间】:2014-06-22 15:19:17
【问题描述】:

我使用 PHP 和 file_get_contents。即使我试图用这个来加速它也真的很慢:

$opts = array(
    'http'=> array(
    'header' => 'Connection: close'
    )
);
$context = stream_context_create($opts);
$contents = file_get_contents('http://www.example.com/file.txt', false, $context);

我也试过cURL。同样的问题。

我读到include 应该比file_get_contents 慢。这似乎只在不包括整个 URL 但包括相对路径时才成立,就像这样......

file_get_contents('../file.txt');

我的问题是……为什么相对路径比完整 URL 快得多?

【问题讨论】:

  • 也许第二个使用本地文件系统,而不是 http?

标签: php url path file-get-contents


【解决方案1】:

file_get_contents 不接受相对 URI。它采用绝对 URI 或文件路径。

使用文件路径更快,因为:

  • 从文件系统中读取

工作量少于:

  • 发出 HTTP 请求
  • 读取请求
  • 从文件系统加载文件(可能在途中执行 PHP)
  • 将其放入 HTTP 响应中
  • 发送响应
  • 读取响应
  • 从响应正文中获取文件数据

【讨论】:

    猜你喜欢
    • 2016-03-26
    • 2012-03-22
    • 2012-11-05
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 2015-07-19
    相关资源
    最近更新 更多