发布 XML 使用 GUZZLE 客户端 LARAVEL

POSTING XML USING GUZZLE CLIENT LARAVEL

我是一名 laravel 初级开发人员,我一直在使用 gazzle http 来处理我的请求,现在我的任务是将集合集成到系统中。提供的 API 只想要我 post XML 数据。当我使用 Json 时,效果很好,但现在我的任务是通过 gazzle posting xml。我该怎么做。

与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??

我前段时间遇到了同样的问题,我使用 AttayToXml 包找到了一个很好的解决方案。您需要做的就是为您的数据创建 array

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

然后,使用 convert() 方法将此数组转换为 xml,在该方法中传递 xml:

的根元素的名称
$xml = ArrayToXml::convert($array, 'AutoCreate');

这将创建您想要的 xml:

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

然后,通过 Guzzle 客户端将它与我在我的项目中使用的类似内容一起发送:

$request = $httpClient->post($yourUrl, [
                    'body' => $xml,
                    'http_errors' => true,
                    'verify' => false,
                    'defaults' => ['verify' => false]
                ]);
<?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