php - file_get_contents 默认使用哪个 HTTP 版本?

标签 php http file-get-contents

最近,我们的一位服务提供商向他们的服务发送了此更新:

使用 HTTP 1.0 协议(protocol):
API 调用将返回 HTTP/1.0 400 Bad Request ... 应将其代码更新为 HTTP 1.1 并在 API 请求中包含主机 header 。

PHP 的 file_get_contents()默认情况下执行所有这些操作吗?

最佳答案

您可以像下面的代码一样将 stream_context 传递给 file_get_contents:

<?php
// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);
?>

您可以在 PHP 手册中找到更多信息,网址为: http://us.php.net/file_get_contents

希望对您有所帮助。

关于php - file_get_contents 默认使用哪个 HTTP 版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21703416/

相关文章:

javascript - 如何使用php查找html中的元素?

php - 使用导入上传 CSV 文件时,仅导入 1 条记录

php - 跨网站更改电话号码

php - 搜索字符串和返回行 PHP

php - 如何在 facebook graph api 请求中使用 cURL

http - 发布与放置与删除

php - 使用 mysql/PHP 进行本地化

php - 如何在我的基于 PHP 的网站中添加 "View more results"类功能?

http - Tomcat http 错误状态标签不同

java - 无法向 API Retrofit 提交帖子