使用PHP发送SOAP请求并获取返回的响应

时间:2015-09-26 10:33:51

标签: php soap

我正在尝试将支付网关集成到一个简单的PHP站点(我自己的站点)中,并且网关仅接受SOAP格式的数据。我完全不知道SOAP是什么,但感谢Google,我现在知道它的样子(至少)。

基本上,我需要将一堆客户数据和支付数据发送到网关,以根据响应接收进行操作。以下是示例请求代码和示例响应代码。他们只提供了发布到的URL,即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>

如何将这些发布到使用PHP提供的URL,如何从返回的响应中获取响应SendTransactionsActionResult

我不是要求你为我做这件事,但像代码这样的简单入门会有很大的帮助 提前致谢

2 个答案:

答案 0 :(得分:0)

您可以使用Curl,php soapClientNuSOAP

对此进行归档

下面我向您展示了如何使用 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>";

答案 1 :(得分:0)

使用http://69.94.141.22/SaveTransactions.asmx?WSDL中的WSDL,您可以从wsdltophp.com生成相应的包,以确保如何在PHP中构造您的请求,因为每个元素都是具有setter / getters的PHP对象。它使用本机PHP SoapClient类,因此,如果您熟悉PHP,您可以轻松快速地了解发送这些请求的人