【发布时间】:2025-11-26 15:35:01
【问题描述】:
我在 Laravel 5.8 项目中使用 Guzzle 已经有一段时间了。它一直在使用支持 JSON 格式的 Restful API。
现在有一个仅支持XML 格式的新Restful API。我不知道如何使用Guzzle 来做到这一点。下面是 HTTP 请求的示例。
POST: http://api.url_endpoint.com/my_api.ashx HTTP/1.1
Content-Type: application/x-www-form-url encoded
Host: http://api.url_endpoint.com
Content-Length: 467
Expect: 100-continue
Connection: Close
<Section>
<LoginDetails>
<Login>ABC</Login>
<Password>ABCDE</Password>
</LoginDetails>
</Section>
在文档中,它说:The XML should be in the body of the request.
问题 1. 如何将 XML 放入请求的正文中?
问题2。注意HTTP/1.1,是否应该将其连接为API URL端点的后缀?
这就是我尝试过的方式。
$header_options = [
'headers' => [
'Accept' => 'application/xml',
'Content-Type' => 'application/x-www-form-url encoded',
'Host' => 'http://api.url_endpoint.com',
'Content-Length' => 467,
'Expect' => '100-continue',
'Connection' => 'Close',
],
'body' => '<Section><LoginDetails><Login>ABC</Login><Password>ABCDE</Password></LoginDetails></Section>',
];
$response = $client->request('POST', 'http://api.url_endpoint.com/my_api.ashx', $header_options);
dump($response->xml());
但我仍然收到 400 Bad Request 作为响应。
【问题讨论】:
标签: php xml laravel api guzzle