使用GUZZLE客户端LARAVEL发布XML
我是一名laravel初级开发人员,我一直在使用gazzle http处理我的请求,现在我的任务是将集合集成到系统中。提供的API只希望我发布XML数据。当我使用Json时,它工作得很好,但现在我的任务是通过gazzle发布xml。我该怎么做呢 使用Json使用GUZZLE客户端LARAVEL发布XML,laravel,guzzle,Laravel,Guzzle,我是一名laravel初级开发人员,我一直在使用gazzle http处理我的请求,现在我的任务是将集合集成到系统中。提供的API只希望我发布XML数据。当我使用Json时,它工作得很好,但现在我的任务是通过gazzle发布xml。我该怎么做呢 使用Json $response = $client->request('POST', 'https://app.apiproviders.com/api/payment/donate', [ 'form_params' =&g
$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??
$response=$client->request('POST','https://app.apiproviders.com/api/payment/donate', [
“表单参数”=>[
“名称”=>“TIG测试”,
“金额”=>美元金额,
'number'=>str_replace('+','$this->senders_contact),
“香奈儿”=>“TIG”,
“转诊”=>str_replace(“+”,“$this->senders_contact)
]
]);
要发布的所需XML格式:
存款基金
500
256702913454
测试API
收据.doc
ASBHBSBWYXLPBMCGNJAWMDAG2HPBGXPBMDZ
如何将此xml传递给laravel中的gazzle??
不久前我遇到了一个相同的问题,我使用这个包找到了一个很好的解决方案。您只需创建数据的数组
:
$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>
让我知道这是否有助于您,或者如果您需要任何其他信息 这里没什么要说的了,谢谢你的帮助,但是,在这样做之后,它会返回一个类似登录失败的响应。这意味着它没有读取我提供的登录凭据。***我没有在上面的示例代码中包含这些凭据***我终于找到了答案。我必须添加'headers'=>['Content Type'=>'text/xml;charset=UTF8',],'body'=>$xml,//等等。谢谢你,很抱歉我之前没有回复,我很忙。没问题,很高兴我能帮忙。如果我的回答帮助你解决了你的问题,那就接受它,这样有同样问题的人会发现它更容易解决。
$request = $httpClient->post($yourUrl, [
'body' => $xml,
'http_errors' => true,
'verify' => false,
'defaults' => ['verify' => false]
]);