HTTP请求在PHP错误请求中失败 [英] HTTP request failed in PHP Bad Request

查看:336
本文介绍了HTTP请求在PHP错误请求中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以直接从浏览器请求URL Web服务'WS',但是当我在代码中使用file_get_contents()或fopen方法时,会收到错误消息.有人不用卷发就能解决问题吗?

I can request a URL web service 'WS' directly from the browser, but when I use file_get_contents() or fopen methods in my code I get an error message. Does someone have a solution without using curl?

public function sendHttpRequest($data) {
   ini_set('user_agent', 'PHP');

    $context_options = array(
        'http' => array(
            'method' => 'POST',
            'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
            'content' => json_encode($data)
        )
    );


    $context = stream_context_create($context_options);

    //line error "line 196"
    $result = file_get_contents($this->WS, false, $context);
}

错误消息:

无法打开流:HTTP请求失败! HTTP/1.1 400错误请求 在/mnt/hgfs/case/src/bat/abc/send-entry.php在第196行

failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /mnt/hgfs/case/src/bat/abc/send-entry.php on line 196

NULL

我已将上下文选项更改为此,但仍然获得了HTTP/1.1 400 Bad Request

i've changed the context options to this, but still getting the HTTP/1.1 400 Bad Request

$context_options = array(
        'http' => array(
            'method' => 'POST',
            'header' => "Date: " . gmdate("D, d M Y H:i:s T", time()) . "\r\n"
            . get_headers($this->WS)[2] . "\r\n"
            . " x-powered-by: PHP/" . phpversion() . "\r\n"
            . " " . get_headers($this->WS)[5] . "\r\n"
            . " Pragma: no-cache" . "\r\n"
            . " Content-Length: " . strlen($this->content) . "\r\n"
            . " Content-Type: text/xml" . "\r\n"
            . " " .get_headers($this->WS)[9] . "\r\n"
            . " Connection: close",
            'content' => $this->content
        )

var_dump(get_headers($this->WS))

数据发送到地址为 http://192.168.xxx.xxx/WS 的服务器

the data is sent to a server with adress http://192.168.xxx.xxx/WS

这是要发送的数组的var_dump

here is a var_dump of the array to send

array(16) {
  ["method"]=>
  string(24) "WS"
  ["LOGIN"]=>
  string(12) "xxx"
  ["DATE1"]=>
  string(10) "1970-01-01"
  ["DATE2"]=>
  string(0) ""
  ["TRANSMISSION"]=>
  INT(8)
  ["REF"]=>
  int(206)
  ["FORMAT"]=>
  int(7)
  ["DOMAIN"]=>
  int(3)
  ["TYPE1"]=>
  int(15)
  ["TYPE2"]=>
  NULL
  ["NAME"]=>
  string(12) "JDOE"
  ["ADRESSE"]=>
  string(0) ""
  ["ADRESSE2"]=>
  string(0) ""
  ["ADRESSE3"]=>
  string(0) ""
  ["ADRESSE4"]=>
  string(0) ""
  ["REF2"]=>
  string(0) ""
}

这是完整的消息错误

警告:file_get_contents( http://192.168.xxx.xxx/WS ):打开失败流:HTTP请求失败! HTTP/1.1 400错误请求 在/mnt/hgfs/case/src/bat/abc/send-entry.php在第182行上

Warning: file_get_contents(http://192.168.xxx.xxx/WS): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /mnt/hgfs/case/src/bat/abc/send-entry.php on line 182

工作代码:

    $context_options1 = array(
        'http' => array(
            'method' => 'POST',
            'header' => "Date: " . gmdate("D, d M Y H:i:s T", time())
            . "Accept: application/xml"
            . "Content-Type: application/xml"
            . "Content-Length: " . strlen($this->content),
            'content' => $this->content
        )
    );

推荐答案

POST请求需要一个Content-Length标头:

$content = json_encode($data);
$context_options = array(
        'http' => array(
            'method' => 'POST',
            'header' => "Content-Type: application/x-www-form-urlencoded\r\n" 
                         . "Content-Length: " . strlen($content) . "\r\n",
            'content' => $content
        )
    );

这显然是由包装器自动完成的,因此还有其他事情.也许省略标题中的最后一个换行符("\ r \ n").

This is apparently done by the wrapper automagically, so there's something else. Perhaps omit the last linebreak ("\r\n") in headers.

这篇关于HTTP请求在PHP错误请求中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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