简体   繁体   中英

How to fix file_get_contents warning

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM