Proper way to send (POST) xml with guzzle 6

user3485417 picture user3485417 · Jan 11, 2016 · Viewed 24.2k times · Source

I want to perform a post with guzzle sending an xml file. I did not find an example.

What I 've done so far is :

$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, [ 'body'=>$xml]);
$response = $client->send($request);
 //
//$code = $response->getStatusCode(); // 200
//$reason = $response->getReasonPhrase(); // OK
 //
 echo $response->getBody();

No matter what I try I get back error -1 which means that xml is not valid. XML that I send passes online validation though and is valid %100

Please help.

Answer

user3485417 picture user3485417 · Jan 12, 2016

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
);