gpt4 book ai didi

php - 如何在 curl 中发送 SOAP 请求

转载 作者:可可西里 更新时间:2023-10-31 23:21:44 25 4
gpt4 key购买 nike

我是 soap 的新手,我该如何发送 soap 请求?我在谷歌中搜索并尝试了不同的方法,但遗憾的是它对我不起作用。

非常感谢您的帮助。

这是我应该发送的示例请求:

POST /Universal/WebService.asmx HTTP/1.1
Host: www.sample.com
Content-Type: text/xml;charset="utf-8"
Content-Length: length
SOAPAction: https://www.sample.com/Universal/WebService.asmx/Request

<?xml version=\"1.0\" encoding=\"utf-8\"?>
<soap:Envelope xmlns:xsi="http://wwww.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Request xmlns="https://www.sample.com/Universal/WebService.asmx">
<ID>CPHK01</ID>
<UID>TEST</UID>
<PWD>TEST</PWD>
<target_mpn>09183530925</target_mpn>
<amount>115</amount>
<ref_no>20060830143030112</ref_no>
<source_mpn>67720014</source_mpn>
</Request>
</soap:Body>
</soap:Envelope>

这是响应:

 HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version = "1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://wwww.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<RequestResponse xmlns="https://www.sample.com/Universal/WebService.asmx">
<RequestResult>
<Ref_no>20060830143030112</Ref_no>
<StatusCode>101</StatusCode>
</RequestResult>
</RequestResponse>
</soap:Body>
</soap:Envelope>

最佳答案

PHP 有一个原生的 SoapClient类(class)。构造函数将 WSDL 作为参数。这优于 cURL,因为 SoapClient 处理所有复杂的 SOAP,它允许您处理 native 对象和数组,并且无需手动构建 SOAP 信封和 XML。

try {
$client = new SoapClient('https://www.sample.com/Universal/WebService.asmx?wsdl');
$response = $client->Request(array(
'ID' => 'xxxx',
'UID' => 'xxxx',
'PWD' => 'xxxx',
'target_mpn' => 'xxxx',
'amount' => 'xxxx',
'ref_no' => 'xxxx',
'source_mpn' => 'xxxx'
));

print_r($response); // view the full response to see what is returned

// or get the response properties:
echo $response->RequestResult->Ref_no;
echo $response->RequestResult->StatusCode;

} catch (Exception $e) {
echo $e->getMessage();
}

关于php - 如何在 curl 中发送 SOAP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17461176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com