Proper way to send (POST) xml with guzzle 6

和自甴很熟 提交于 2019-12-03 10:42:13
user3485417

After some experiments, I have figured it out. Here is my solution in case someone reaches a dead end.

$request = new Request(
    'POST', 
    $uri,
    ['Content-Type' => 'text/xml; charset=UTF8'],
    $xml
);
Abz

This is what worked for me on Guzzle 6:

// configure options
$options = [
    'headers' => [
        'Content-Type' => 'text/xml; charset=UTF8',
    ],
    'body' => $xml,
];

$response = $client->request('POST', $url, $options);
Shaolin

If you want to send xml using the post method, here is an example:

$guzzle->post($url, ['body' => $xmlContent]);

You can do that in a below way

$xml_body = 'Your xml body';
$request_uri = 'your uri'
$client = new Client();
$response = $client->request('POST', $request_uri, [
              'headers' => [
                 'Content-Type' => 'text/xml'
               ],
              'body'   => $xml_body
            ]);

Try posting the data like:

$xml2=simplexml_load_string($xml) or die("Error: Cannot create object");
use    GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
$client = new Client();
//
$request = new Request('POST', $uri, [
'form_params' => [
        'xml' => $xml,
    ]
]);
$response = $client->send($request);
//$code = $response->getStatusCode(); // 200
//$reason = $response->getReasonPhrase(); // OK
echo $response->getBody();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!