【问题标题】:POSTING XML USING GUZZLE CLIENT LARAVEL使用 GUZZLE 客户端 LARAVEL 发布 XML
【发布时间】:2020-06-04 16:32:55
【问题描述】:

我是一名 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】:

前段时间我遇到了同样的问题,我找到了一个很好的解决方案,使用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]
                ]);

【讨论】:

  • 感谢您的帮助,但是,现在这样做之后,它会返回类似登录失败的响应。这意味着它没有读取我提供的登录凭据。 ***我没有在上面的示例代码中包含这些凭据***
  • 我终于想通了。我必须添加'标题'=> ['Content-Type' =>'text/xml; charset=UTF8', ], 'body' => $xml, //etc.谢谢
  • 对不起,我没有早点回复,很忙。没问题,很高兴能帮上忙。如果我的回答帮助您解决了您的问题,请接受它,这样有相同问题的人可以更轻松地找到它。
猜你喜欢
  • 2021-12-26
  • 2020-11-20
  • 1970-01-01
  • 2017-01-09
  • 1970-01-01
  • 2021-01-06
  • 2015-06-09
  • 2016-06-24
  • 2023-03-14
相关资源
最近更新 更多