如何修复 file_get_contents 警告

发布于 12-21 00:45 字数 460 浏览 2 评论 0原文

我在 Google checkout 网关处理程序中使用以下代码,但是当我测试它时,它显示以下警告:

警告:file_get_contents(https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/XXXXXXXXXXX) [function.file-get-contents]:无法打开流:HTTP 请求失败! HTTP/1.0 411 长度需要在 /sample.php 第 116 行

我的第 116 行如下所示:

//some more code//
$ctx = stream_context_create($params);
echo file_get_contents($_googleUrl, false, $ctx);
exit;

有人可以帮助我吗?

I am using following code in Google checkout gateway handler, but when I test it, it shows below warning:

Warning: file_get_contents(https://checkout.google.com/api/checkout/v2/checkoutForm/Merchant/XXXXXXXXXXX) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 411 Length Required in /sample.php on line 116

My line 116 looks like below:

//some more code//
$ctx = stream_context_create($params);
echo file_get_contents($_googleUrl, false, $ctx);
exit;

Can somebody help me here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦笑流年记忆2024-12-28 00:45:23

来自 w3.org

需要 411 长度

服务器拒绝接受没有定义的请求
内容长度。如果客户端添加了有效的内容,则可以重复该请求
Content-Length头字段包含实体主体的长度
在请求消息中。

这可能意味着您在请求中没有发送或发送了不正确的 Content-Length 标头。如果不知道 $params 数组的内容,就很难调试它。因此,如果您发送的内容不正确,则需要删除 Content-Length 标头并让 PHP 自动处理它(假设 PHP 会执行此操作),或者如果您当前没有发送它,发送正确的一个。正如其他人所建议的,cURL 可能是执行此操作的更可行的选择。

另外,进行更多研究表明,当您使用 POST 但不提供任何 POST 变量时,某些服务器会出现这种行为(错误)。因此,如果是这种情况,您可以通过两种方法之一解决此问题。

  1. 使用 GET 而不是 POST(首选)。
  2. 如果您无法使用 GET,请提供一些虚拟 POST 变量(如果您不发送任何变量)。

From w3.org

411 Length Required

The server refuses to accept the request without a defined
Content-Length. The client may repeat the request if it adds a valid
Content-Length header field containing the length of the entity body
in the request message.

This could mean that you are either not sending or sending an incorrect Content-Length header in your request. Without knowing the contents of your $params array it is difficult to debug this. So you will either need to get rid of the Content-Length header if you are sending an incorrect one and let PHP handle it automatically (assuming PHP will do this) or if you are not currently sending it, send a correct one. As others have suggested, cURL may be a more viable option for doing this.

Also, doing a bit more research shows that some servers (mis)behave this way when you use POST, but do not provide any POST variables. You can therefore solve this in one of two ways if this is the case.

  1. Use GET instead of POST (preferred).
  2. If you cannot use GET, provide some dummy POST variables if you are not sending any.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文