PHP stream_context_create和HTTP头 - 数组或字符串,\ r \ n在结尾,Content-Length可选? [英] PHP stream_context_create and HTTP headers - array or string, \r\n at the end, Content-Length optional?

查看:625
本文介绍了PHP stream_context_create和HTTP头 - 数组或字符串,\ r \ n在结尾,Content-Length可选?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解决发布到远程站点时遇到的一些问题,特别是远程主机从不返回任何数据(空字符串)。



在我尝试排除其他任何问题之前,我想确保调用代码实际上是正确的。代码是:
$ b $ pre $ $ $ $ c $ $ $ context $ stream = =>POST,
'header'=>Content-Type:application / xml,
'timeout'=> 60.0,
'ignore_errors'=> true ,#即使HTTP状态!= 200
'content'=> $ send_xml
)));

$ response = trim(file_get_contents($ this-> bulk_service_url,false,$ context));

我的所有问题都属于标题选项,它的值,以及如何正确格式化它,写下来。 PHP文档,它下面的讨论,甚至是stackoverflow的研究都会产生非常不一致的结果。1)我是否必须包含Content-Length头部,如果不是,PHP会正确计算它? 文档不包括它,但我看到很多人手动包含它,是它受到了PHP的尊重或覆盖?



2)我是否必须将字头选项作为字符串或关联数组传递?手册说字符串,大多数传递它作为一个字符串,但此评论说,如果PHP是使用--with-curlwrappers选项编译的,则必须将其作为数组传递。这是非常不一致的行为。

3)作为字符串传递时,是否必须包含终止 \r\\\
字符?特别是只指定一个标题时。手册没有提供这样的例子,手册页上的第一条评论确实包含了它,第二条评论对于如何指定它没有明确的规定。 PHP会自动处理这两种情况吗?



服务器使用的是PHP 5.3。 解决方案

您应该将您的标题作为数组存储在代码中,并在发送请求之前完成准备工作......

  function prepareHeaders($ headers){
$ flattened = array();

foreach($ headers为$ key => $ header){
if(is_int($ key)){
$ flattened [] = $ header;
} else {
$ flattened [] = $ key。':'。$ header;
}
}

返回implode(\r\\\
,$ flattened);


$ header = array(
'Content-Type'=>'application / xml',
'ContentLength'=> $ dl,
);

$ context = stream_context_create(array('http'=> array(
'method'=>POST,
'header'=> prepareHeaders($
'timeout'=> 60.0,
'ignore_errors'=> true,
'content'=> $ send_xml
)));

$ response = trim(file_get_contents($ url,FALSE,$ context));


I am troubleshooting some problems with POSTing to a remote site, specifically the remote host never returns any data (empty string).

Before I try to troubleshoot anything else, I want to make sure the calling code is actually correct. The code is:

$context = stream_context_create(array('http' => array(
    'method'        => "POST",
    'header'        => "Content-Type: application/xml",
    'timeout'       => 60.0,
    'ignore_errors' => true, # return body even if HTTP status != 200
    'content'       => $send_xml
)));

$response = trim(file_get_contents($this->bulk_service_url, false, $context));

All my questions belong to the "header" option and it's values, and how to correctly format it and write it. The PHP documentation, discussion below it and even stackoverflow research yield very inconsistent results.

1) do I have to include the Content-Length header, and if not, will PHP calculate it correctly? The documentation does not include it, but I've seen many people include it manually, is it then respected or overwritten by PHP?

2) do I have to pass the header option as a string, or an associative array? Manual says string, majority pass it as a string, but this comment says that if PHP was compiled with --with-curlwrappers option, you have to pass it as an array. This is very inconsistent behavior.

3) when passing as a string, do I have to include terminating \r\n characters? Especially when specifying just one header. Manual does not provide such an example, first comment on manual page does include it, second one does not, again, no clear rule on how to specify this. Does PHP automatically handle both cases?

The server is using PHP 5.3.

解决方案

You should really store your headers within code as an array and finalize the preparation just prior to sending the request...

function prepareHeaders($headers) {
  $flattened = array();

  foreach ($headers as $key => $header) {
    if (is_int($key)) {
      $flattened[] = $header;
    } else {
      $flattened[] = $key.': '.$header;
    }
  }

  return implode("\r\n", $flattened);
}

$headers = array(
  'Content-Type' => 'application/xml',
  'ContentLength' => $dl,
);

$context = stream_context_create(array('http' => array(
  'method'        => "POST",
  'header'        => prepareHeaders($headers),
  'timeout'       => 60.0,
  'ignore_errors' => true,
  'content'       => $send_xml
)));

$response = trim(file_get_contents($url, FALSE, $context));

这篇关于PHP stream_context_create和HTTP头 - 数组或字符串,\ r \ n在结尾,Content-Length可选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆