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.
Sending Requests You can create a request and then send the request with the client when you're ready: use GuzzleHttp\Psr7\Request; $request = new Request('PUT', 'http://httpbin.org/put'); $response = $client->send($request, ['timeout' => 2]);
To post XML data to the server, you need to make an HTTP POST request, include the XML in the body of the request message, and set the correct MIME type for the XML. The correct MIME type for XML is application/xml.
You can check to see if a request or response has a body using the getBody() method: $response = GuzzleHttp\get('http://httpbin.org/get'); if ($response->getBody()) { echo $response->getBody(); // JSON string: { ... } } The body used in request and response objects is a GuzzleHttp\Stream\StreamInterface .
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With