PHP无法使用超过1024个字符的file_get_contents()内容进行POST

时间:2014-04-08 10:06:38

标签: php limit file-get-contents

我在使用file_get_contents()时遇到了问题。

POST使用file_get_contents()将数据格式化为远程脚本,并且适用于短格式(即$postdata < 1024),但是当我的表单内容超过1024时} chars($postdata > 1024)然后file_get_contents()与远程脚本通信时出现问题,总是返回一个空字符串(非假)

并且远程脚本没有任何问题,因为测试我甚至修改它只返回“OK”,但正如我对$postdata > 1024说的那样 - 我得到的只是一个空字符串(和遥控器)脚本没有做它应该做的事情)

这是代码:

$postvals = array(
  'from' => 'Test <test@test.com>',
  'to' => 'test2@test.com,
  'subject' => 'Test',
  'body' => $mail_html_content
);

$postdata = http_build_query($postvals);

$opts = array('http' =>
  array(
    'method'  => 'POST',
    'header'  => array(
      'Connection: close',
      'Content-type: application/x-www-form-urlencoded',
      'Content-Length: '.strlen($postdata),
      'Custom-header: test'),
    'content' => $postdata,
    'timeout' => 40
  )
);

$context  = stream_context_create($opts);
$result = file_get_contents('http://test.com/script.php', false, $context, -1, 40000);

BTW我的服务器post_max_size设置为100M

服务器的

phpinfo()未报告任何1024或1k值(log_errors_max_len 1024除外)

这个脚本可能存在问题? (我不喜欢使用curl)

1 个答案:

答案 0 :(得分:0)

我已经在我的服务器上使用50Mb文件测试了您的脚本,并且它有几个故障。

  1. 我设置了post_max_size = 200Mb
  2. 我设置了memory_limit = -1(我有很多)
  3. 请记住,您需要内存来打开文件,然后在http_build_query()中复制并编码该字符串。它花了差不多300Mb 的内存,我说你需要至少两倍的100Mb文件。此外,您在处理帖子的另一侧需要大约相同的金额。那么,你的另一面是同一台机器,所以共享相同的内存,我在想的是你的内存不足。
  4. 您的'to'密钥缺少一个尾随引用。
  5. 同样,代码通常有效,因此请确保您有足够的RAM,如果可能的话,请尝试使用其他服务器。