如何使用 Laravel 使用 Guzzle 将 XML POST 请求发送到 Web 服务 API?
How to send XML POST request with Guzzle to web service API using Laravel?
我正在尝试 post 使用 Laravel Guzzle Http 客户端向我的 Web API 发出请求。但是,我在尝试 post 请求时遇到错误。我要发送的数据是 XML,因为 API 控制器是以 XML return 格式构建的。
我已经尝试了各种方法来 post Guzzle 的请求,但它还没有奏效。
public function createProperty(Request $request)
{
$client = new Client();
$post = $request->all();
$create = $client->request('POST', 'http://127.0.0.1:5111/admin/hotel', [
'headers' => [
'Content-Type' => 'text/xml; charset=UTF8',
],
'form-data' => [
'Name' => $post['hotel_name'],
'Address' => $post['address'],
'Phone' => $post['phone'],
'Email' => $post['email'],
'Website' => $post['website'],
'Latitude' => $post['latitude'],
'Longitude' => $post['longitude'],
'Tags' => $post['tags'],
'Priority' => $post['priority'],
'Visible' => $post['visible'],
'Stars' => $post['stars'],
'Description' => $post['description'],
'Facilities' => $post['facilities'],
'Policies' => $post['policies'],
'ImportantInfo' => $post['important_info'],
'MinimumAge' => $post['minimum_age']
]
]);
//dd($create->getBody());
echo $create->getStatusCode();
echo $create->getHeader('content-type');
echo $create->getBody();
$response = $client->send($create);
$xml_string = preg_replace('/(<\?xml[^?]+?)utf-16/i', 'utf-8', $create->getBody());
$xml_string = $create->getBody();
//dd($xml_string);
$hotels = simplexml_load_string($xml_string);
return redirect()->back();
}
我希望结果 POST 到网络服务并将数据保存到数据库,但是我收到错误“客户端错误:POST 'http://127.0.0.1:5111/admin/hotel' 导致“400 错误请求”响应。请在正文中提供有效的 XML 对象
而不是在 guzzle 请求中使用 post-data
,您需要使用 body
:
$create = $client->request('POST', 'http://127.0.0.1:5111/admin/hotel', [
'headers' => [
'Content-Type' => 'text/xml; charset=UTF8',
],
'body' => $xml
]);
$xml
将是您要发送到 API 的 XML 数据。 Guzzle 不会为您创建 XML 数据,您需要自己创建。
可以使用 DomDocument
class in PHP 创建 XML 数据。
如果您使用的是 Laravel 7+,那么这条简单的线应该可以很好地工作
$xml = "<?xml version='1.0' encoding='utf-8'?><body></body>";
Http::withHeaders(["Content-Type" => "text/xml;charset=utf-8"])
->post('https://destination.url/api/action', ['body' => $xml]);
我正在尝试 post 使用 Laravel Guzzle Http 客户端向我的 Web API 发出请求。但是,我在尝试 post 请求时遇到错误。我要发送的数据是 XML,因为 API 控制器是以 XML return 格式构建的。
我已经尝试了各种方法来 post Guzzle 的请求,但它还没有奏效。
public function createProperty(Request $request)
{
$client = new Client();
$post = $request->all();
$create = $client->request('POST', 'http://127.0.0.1:5111/admin/hotel', [
'headers' => [
'Content-Type' => 'text/xml; charset=UTF8',
],
'form-data' => [
'Name' => $post['hotel_name'],
'Address' => $post['address'],
'Phone' => $post['phone'],
'Email' => $post['email'],
'Website' => $post['website'],
'Latitude' => $post['latitude'],
'Longitude' => $post['longitude'],
'Tags' => $post['tags'],
'Priority' => $post['priority'],
'Visible' => $post['visible'],
'Stars' => $post['stars'],
'Description' => $post['description'],
'Facilities' => $post['facilities'],
'Policies' => $post['policies'],
'ImportantInfo' => $post['important_info'],
'MinimumAge' => $post['minimum_age']
]
]);
//dd($create->getBody());
echo $create->getStatusCode();
echo $create->getHeader('content-type');
echo $create->getBody();
$response = $client->send($create);
$xml_string = preg_replace('/(<\?xml[^?]+?)utf-16/i', 'utf-8', $create->getBody());
$xml_string = $create->getBody();
//dd($xml_string);
$hotels = simplexml_load_string($xml_string);
return redirect()->back();
}
我希望结果 POST 到网络服务并将数据保存到数据库,但是我收到错误“客户端错误:POST 'http://127.0.0.1:5111/admin/hotel' 导致“400 错误请求”响应。请在正文中提供有效的 XML 对象
而不是在 guzzle 请求中使用 post-data
,您需要使用 body
:
$create = $client->request('POST', 'http://127.0.0.1:5111/admin/hotel', [
'headers' => [
'Content-Type' => 'text/xml; charset=UTF8',
],
'body' => $xml
]);
$xml
将是您要发送到 API 的 XML 数据。 Guzzle 不会为您创建 XML 数据,您需要自己创建。
可以使用 DomDocument
class in PHP 创建 XML 数据。
如果您使用的是 Laravel 7+,那么这条简单的线应该可以很好地工作
$xml = "<?xml version='1.0' encoding='utf-8'?><body></body>";
Http::withHeaders(["Content-Type" => "text/xml;charset=utf-8"])
->post('https://destination.url/api/action', ['body' => $xml]);