使用PHP发送SOAP请求并获取返回的响应
问题描述:
我正在尝试将支付网关集成到简单的PHP站点(我自己的站点)中,并且网关只接受SOAP格式的数据。我完全不知道SOAP是什么,但感谢谷歌,我现在知道它是怎么样的(至少)。使用PHP发送SOAP请求并获取返回的响应
基本上,我需要发送一堆客户数据和支付数据到网关根据收到的响应行事。以下是示例请求代码和示例响应代码。他们只提供要发布到的网址,即http://69.94.141.22/SaveTransactions.asmx
。
请求
POST /SaveTransactions.asmx HTTP/1.1
Host: 69.94.141.22
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<SendTransactionsAction xmlns="http://tempuri.org/">
<strUserName>string</strUserName>
<strPassword>string</strPassword>
<strSecureKey>string</strSecureKey>
<strFirstName>string</strFirstName>
<strLastName>string</strLastName>
<strPhoneNumber>string</strPhoneNumber>
<strStreetNumber>string</strStreetNumber>
<strUnitNumber>string</strUnitNumber>
<strStreetName>string</strStreetName>
<strCity>string</strCity>
<strState>string</strState>
<strZipCode>string</strZipCode>
<strEmailAddress>string</strEmailAddress>
<strBankName>string</strBankName>
<strRoutingNo>string</strRoutingNo>
<strAccountNumber>string</strAccountNumber>
<strCheckNo>string</strCheckNo>
<strAmount>string</strAmount>
<strNotes>string</strNotes>
</SendTransactionsAction>
</soap12:Body>
</soap12:Envelope>
响应
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<SendTransactionsActionResponse xmlns="http://tempuri.org/">
<SendTransactionsActionResult>string</SendTransactionsActionResult>
</SendTransactionsActionResponse>
</soap12:Body>
</soap12:Envelope>
如何发布这些到URL提供了使用PHP和如何获取该响应SendTransactionsActionResult
从返回的响应?
我不是要求你为我做这件事,而是像代码一样简单的开始会有很大的帮助。
在此先感谢
答
您可以通过使用Curl,PHP soapClient或NuSOAP
归档此下面我向您展示如何使用的NuSOAP库
另外,WSDL是http://69.94.141.22/SaveTransactions.asmx?WSDL位置
$client = new nusoap_client("http://69.94.141.22/SaveTransactions.asmx?WSDL", true); // this should be the wsdl location
$error = $client->getError();
if ($error) {
echo "<h2>Constructor error</h2><pre>" . $error . "</pre>";
}
// Use basic authentication method
$client->setCredentials("username", "password", "basic"); //set here the credentials if need for the wsdl
$client->setHeaders('<SendTransactionsAction xmlns="http://tempuri.org/">
<strUserName>string</strUserName>
<strPassword>string</strPassword>
<strSecureKey>string</strSecureKey>
<strFirstName>string</strFirstName>
<strLastName>string</strLastName>
<strPhoneNumber>string</strPhoneNumber>
<strStreetNumber>string</strStreetNumber>
<strUnitNumber>string</strUnitNumber>
<strStreetName>string</strStreetName>
<strCity>string</strCity>
<strState>string</strState>
<strZipCode>string</strZipCode>
<strEmailAddress>string</strEmailAddress>
<strBankName>string</strBankName>
<strRoutingNo>string</strRoutingNo>
<strAccountNumber>string</strAccountNumber>
<strCheckNo>string</strCheckNo>
<strAmount>string</strAmount>
<strNotes>string</strNotes>
</SendTransactionsAction>
');
$result = "";
if ($client) {
$result = $client->call("SendTransactionsAction"); // soap action
}
if ($client->fault) {
echo "<h2>Fault</h2><pre>";
print_r($result);
echo "</pre>";
}
else {
$error = $client->getError();
if ($error) {
echo "<h2>Error</h2><pre>" . $error . "</pre>";
}
else {
echo "<h2>zip code</h2><pre>";
print_r($result);
echo "</pre>";
}
}
echo "<h2>Request</h2>";
echo "<pre>" . htmlspecialchars($client->request, ENT_QUOTES) . "</pre>";
echo "<h2>Response</h2>";
echo "<pre>" . htmlspecialchars($client->response, ENT_QUOTES) . "</pre>";
答
使用来自http://69.94.141.22/SaveTransactions.asmx?WSDL的WSDL,您可以从wsdltophp.com生成相应的包,以确定如何在PHP中构建您的请求,因为每个元素都将成为带有setter/getters的PHP对象。它使用原生的PHP SoapClient类,因此如果您熟悉PHP
,您就可以轻松快速地理解发送这些请求的人员