简体   繁体   English

耗时POST请求导致500错误,即使该方法的Curl起作用

[英]Guzzle POST request results in 500 error even though Curl for that works

I have the below chunk of PHP code for hitting a POST API 我有以下用于击中POST API的PHP代码块

$client = new \GuzzleHttp\Client(['debug'=>true]);
$response = $client->request(
    $request_method,
    $request_url,
    [
        'json' => $request_data,
        'http_errors' => false
    ]
);

$http_code = $response->getStatusCode();

This results in a 500 response from the server, despite all Headers being properly listed in the debug logs as below: 尽管在调试日志中正确列出了所有标头,但仍导致来自服务器的500响应,如下所示:

* Hostname was NOT found in DNS cache
*   Trying 52.xxx.xx.xx...
* Connected to <<hostname>> (52.xxx.xx.xx) port 80 (#0)
> POST /api/rest/auth_key HTTP/1.1
User-Agent: GuzzleHttp/6.2.1 curl/7.38.0 PHP/7.0.9-1~dotdeb+8.1
Content-Type: application/json
Host: <<hostname>>
Content-Length: 42

* upload completely sent off: 42 out of 42 bytes

$request_url above is the complete url for the POST request. 上面的$ request_url是POST请求的完整URL。
$request_data is JSON content (PHP associative array) to be sent. $ request_data是要发送的JSON内容(PHP关联数组)。
$request_method is 'POST' . $ request_method'POST'

Even if I remove the json data from the request, expecting to get a 400 response from the API, I still get the same 500 error. 即使我从请求中删除json数据,并期望从API获得400响应,我仍然会遇到同样的500错误。 The same request when triggered via Curl works fine with the following code: 通过Curl触发的相同请求可以在以下代码中正常工作:

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $request_url,
  CURLOPT_CUSTOMREQUEST => $request_method,
  CURLOPT_POSTFIELDS => json_encode($request_data),
  CURLOPT_HTTPHEADER => [
    "content-type: application/json"
  ],
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Have been trying to debug this for quite some time & even looked for similar references in SO but to no avail. 已经尝试调试了很长时间,甚至在SO中寻找类似的参考资料,但均无济于事。 Any helps in this regard would be great. 在这方面的任何帮助将是巨大的。

I'm not able to comment, since I don't have enough reputation, so if this does not answer your question or resolve the issue, sorry in advance. 由于我的声誉不高,因此我无法发表评论,因此,如果这不能回答您的问题或无法解决问题,请提前抱歉。

I had been experiencing a similar issue, where cURL was working no problem, but I was getting either a 500 response or a 400 Bad Request (Request Too Long). 我曾经遇到过类似的问题,其中cURL正常工作,但是却收到500个响应或400个错误的请求(请求太长)。 It turns out that when you include json data in the third parameter for a POST request, it is being included in the request's header. 事实证明,当您在POST请求的第三个参数中包含json数据时,该数据将被包含在请求的标头中。

The documentation has some examples where the fourth parameter of the GuzzleHttp\\Psr7\\Request class is used as the body of the request (see http://docs.guzzlephp.org/en/stable/psr7.html ). 该文档提供了一些示例,其中将GuzzleHttp\\Psr7\\Request类的第四个参数用作请求的主体(请参见http://docs.guzzlephp.org/en/stable/psr7.html )。 Once I used this and set the content type explicitly in the header, my issues were resolved. 一旦使用了它并在标题中显式设置了内容类型,我的问题就解决了。

That means that your request would look like this: 这意味着您的请求将如下所示:

$response = $client->request(
    $request_method,
    $request_url,
    ['Content-Type'=>'application/json'],
    json_encode($request_data)
);

Hopefully this will help you out too! 希望这也会对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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