【问题标题】:How to fix 411 Length Required error with file_get_contents and the expedia XML API? [closed]如何使用 file_get_contents 和 expedia XML API 修复 411 Length Required 错误? [关闭]
【发布时间】:2012-02-23 11:59:51
【问题描述】:

我正在使用 xml api(php) 项目进行在线酒店预订。当我在预订代码中工作时,它显示以下错误

"Warning: file_get_contents(https://...@gmail.com</email><firstName>test</firstName><lastName>smith</lastName><homePhone>8870606867</homePhone><creditCardType>CA</creditCardType><creditCardNumber>5401999999999999</creditCardNumber>....</HotelRoomReservationRequest>) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 411 Length Required"

这是我的代码

$context  = stream_context_create(
                array(
                    'http' => array(
                        'method' => 'POST',
                        'header' => "Content-type: application/x-www-form-urlencoded",
                        "Accept: application/xml"
                    )
                )
            );
$url ='https://book.api.ean.com/....';
$xml = file_get_contents($url, false, $context);

用于发送信用信息,请给我建议什么类型的错误...。

【问题讨论】:

  • 您设置流上下文选项的方式有误。
  • 谢谢请纠正我的代码提前谢谢
  • stackoverflow 不允许发布代码。我已经提供了足够的代码

标签: php xml api http


【解决方案1】:

根据RFC2616 10.4.12

10.4.12 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 message-body
   in the request message.

您需要将Content-Length 标头添加到您的POST 请求中。这是 POST 请求正文的字节(八位字节)大小。要获得长度,您可以在 POST 正文上使用 strlen。由于您的代码示例没有显示任何 POST 正文,因此很难给出具体示例。帖子正文通过流上下文中的 ['http']['content'] 条目传递。

如果您设置content entry(参见HTTP context options­Docs),可能已经足够了。

编辑:以下示例代码可能解决您的问题。它演示了如何使用file_get_contents 并设置包括Content-Length 标头在内的标头,通过POST 请求将一些XML 发送到服务器。

$url = 'https://api.example.com/action';
$requestXML = '<xml><!-- ... the xml you want to post to the server... --></xml>';
$requestHeaders = array(
    'Content-type: application/x-www-form-urlencoded',
    'Accept: application/xml',
    sprintf('Content-Length: %d', strlen($requestXML));
);

$context = stream_context_create(
                array(
                    'http' => array(
                        'method'  => 'POST',
                        'header'  => implode("\r\n", $requestHeaders),
                        'content' => $requestXML,
                    )
                )
            );
$responseXML = file_get_contents($url, false, $context);

if (FALSE === $responseXML)
{
    throw new RuntimeException('HTTP request failed.');
}

如果您需要更好的错误控制,请参阅ignore_errors HTTP context option­Docs$http_response_header­Docs。 HTTP 响应头的详细处理可以在我的一篇博文中找到:HEAD first with PHP Streams

【讨论】:

  • 如何为我的代码做内容长度请帮助我
  • @user1117625:你需要更改你的代码,它不完整,我无法执行它,所以我无法重现。对不起。
  • 你有没有读过 hakre 写的东西? Content-Length 被提到了 2 次(甚至为了你而着色)。不需要天才就能弄清楚该做什么以及该放在哪里。这些天人们真的不花精力去寻找解决方案吗?
  • $url ='book.api.ean.com/ean-services/rs/hotel/v3/res?...... $str=strlen($url); $context = stream_context_create(array('http' => array('method' => 'POST','header' => "Content-type: application/x-www-form-urlencoded","Accept:application/xml ",'内容长度:$str')));我的代码是否正确检查它
  • 代码是否正确?stream_context_create(array('http' => array('method' => 'POST','header' => "Content-type: application/x-www-form -urlencoded","Accept:application/xml",'Content-Length:$str')));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多