使用GUZZLE CLIENT LARAVEL发布XML

问题描述 投票:0回答:1

我是laravel初级开发人员,我一直在使用gazzle http来处理我的请求,现在我的任务是将Collections集成到系统中。提供的API只希望我发布XML数据。当我使用Json时,它运作良好,但是现在我的任务是通过gazzle发布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??
laravel guzzle
1个回答
0
投票

不久前我遇到了同样的问题,并且使用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]
                ]);

让我知道这是否对您有帮助,或者您是否需要其他信息。

© www.soinside.com 2019 - 2024. All rights reserved.