'POSTING XML USING GUZZLE CLIENT LARAVEL

I am a laravel junior developer and i have been using gazzle http for handling my requests, now i have a task of integrating Collections to a system. The provided API only wants me to post XML data. When i use Json, it works well, but now i have a task of posting xml through gazzle. how can i do it.

with Json,

$response = $client->request('POST', 'https://app.apiproviders.com/api/payment/donate', [
        'form_params'   => [
        'name'          => 'TIG Test',
        'amount'        => $amount,
        'number'        => str_replace('+', '',$this->senders_contact),
        'chanel'        => 'TIG',
        'referral'      => str_replace('+', '',$this->senders_contact)
        ]
        ]);  

the desired XML format to post:

<?xml version="1.0" encoding="UTF-8"?>
<AutoCreate>
    <Request>
        <Method>acdepositfunds</Method>
        <NonBlocking></NonBlocking>
        <Amount>500</Amount>
        <Account>256702913454</Account>
        <AccountProviderCode></AccountProviderCode>
        <Narrative>Testing the API</Narrative>
        <NarrativeFileName>receipt.doc</NarrativeFileName>
        <NarrativeFileBase64>aSBhbSBwYXlpbmcgNjAwMDAgc2hpbGxpbmdz</NarrativeFileBase64>
    </Request>
</AutoCreate>

how can i pass this xml to gazzle in laravel??


Solution 1:[1]

I had a same problem a while ago, and I found a good solution using AttayToXml package. All you need to do is to create an array of your data:

$array = [
    'Request' => [
        'Method' => 'value',
        'NonBlocking' => 'value',
        'Amount' => 'value',
        //and so on...
    ]
];

Then, you convert this array to xml, using the convert() method, where you pass the name of the root element of your xml:

$xml = ArrayToXml::convert($array, 'AutoCreate');

And this will create your desired xml:

<AutoCreate>
    <Request>
        <Method>acdepositfunds</Method>
        <NonBlocking></NonBlocking>
        <Amount>500</Amount>
        //and so on...
    </Request>
</AutoCreate>

And then, send it through Guzzle client with something like this, which I used in on of my projects:

$request = $httpClient->post($yourUrl, [
                    'body' => $xml,
                    'http_errors' => true,
                    'verify' => false,
                    'defaults' => ['verify' => false]
                ]);

Solution 2:[2]

<?php
~~~

use Illuminate\Support\Facades\Http;

~~~

$xml = new \SimpleXMLElement('<Hogeattribute></Hogeattribute>');
$xml->addChild('hogeId', 'hoge1');

$response = Http::withBody(
    $xml->asXML(),
    'text/xml'
)->post('https://hoge');

$json = json_encode(simplexml_load_string($response->body()));
$data = json_decode($json, true);

https://readouble.com/laravel/8.x/ja/http-client.html https://qiita.com/megponfire/items/e2cfa22216073dd5d596

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nimantha
Solution 2 porin