Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to send (POST) xml with guzzle 6

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.

like image 740
user3485417 Avatar asked Jan 11 '16 16:01

user3485417


People also ask

How do I send a post request using guzzle?

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

How do I send a post request in XML?

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.

How do I get responses from GuzzleHttp?

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 .


1 Answers

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);
like image 199
Abdullah Aman Avatar answered Sep 20 '22 20:09

Abdullah Aman