简体   繁体   English

PHP->卷曲->肥皂响应(从xml标签获取数据)

[英]PHP -> Curl -> Soap Response (Get data from xml tags)

I have a Curl response with Soap XML data. 我有一个Soap XML数据的Curl响应。

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/><SOAP-ENV:Body><ns2:GetAccountMovementsResponseIo xmlns:ns2="http://www.mygemini.com/schemas/mygemini">
<ns2:result>
    <ns2:pager>
        <ns2:pageIndex>0</ns2:pageIndex>
        <ns2:pageSize>700</ns2:pageSize>
    </ns2:pager>
    <ns2:totalCount>3</ns2:totalCount>
</ns2:result>
<ns2:accountMovement>
    <ns2:movementId>002147575330.2</ns2:movementId>
    <ns2:paymentId>95694091</ns2:paymentId>
    <ns2:externalPaymentId>2147575330</ns2:externalPaymentId>
    <ns2:debitCredit>1</ns2:debitCredit>
    <ns2:valueDate>2018-12-13T00:00:00+04:00</ns2:valueDate>
    <ns2:description>გადარიცხვა პირად ანგარიშზე</ns2:description>
    <ns2:amount>
        <ns2:amount>10000</ns2:amount>
        <ns2:currency>GEL</ns2:currency>
    </ns2:amount>
    <ns2:accountNumber>GE44TB7142536020100005</ns2:accountNumber>
    <ns2:accountName>კლიენტი:1425</ns2:accountName>
    <ns2:additionalInformation>კლიენტი:1425, 123456786, TBCBGE22, GE17TB7142536020100006</ns2:additionalInformation>
    <ns2:documentDate>2018-12-13T00:00:00+04:00</ns2:documentDate>
    <ns2:documentNumber>1544698824</ns2:documentNumber>
    <ns2:partnerAccountNumber>GE17TB7142536020100006</ns2:partnerAccountNumber>
    <ns2:partnerName>კლიენტი:1425, 123456786</ns2:partnerName>
    <ns2:partnerTaxCode>123456786</ns2:partnerTaxCode>
    <ns2:partnerBankCode>TBCBGE22</ns2:partnerBankCode>
    <ns2:partnerBank>სს  თიბისი  ბანკი</ns2:partnerBank>
    <ns2:taxpayerCode>123456786</ns2:taxpayerCode>
    <ns2:taxpayerName>კლიენტი:1425, 123456786</ns2:taxpayerName>
    <ns2:operationCode>GII</ns2:operationCode>
    <ns2:partnerDocumentType>0</ns2:partnerDocumentType>
    <ns2:statusCode>3</ns2:statusCode>
    <ns2:transactionType>1</ns2:transactionType>
</ns2:accountMovement>
<ns2:accountMovement>
    <ns2:movementId>002147575330.1</ns2:movementId>
    <ns2:paymentId>95694091</ns2:paymentId>
    <ns2:externalPaymentId>2147575330</ns2:externalPaymentId>
    <ns2:debitCredit>0</ns2:debitCredit>
    <ns2:valueDate>2018-12-13T00:00:00+04:00</ns2:valueDate>
    <ns2:description>გადარიცხვა პირად ანგარიშზე</ns2:description>
    <ns2:amount>
        <ns2:amount>10000</ns2:amount>
        <ns2:currency>GEL</ns2:currency>
    </ns2:amount>
    <ns2:accountNumber>GE17TB7142536020100006</ns2:accountNumber>
    <ns2:accountName>კლიენტი:1425, 123456786</ns2:accountName>
    <ns2:additionalInformation>კლიენტი:1425, TBCBGE22, GE44TB7142536020100005</ns2:additionalInformation>
    <ns2:documentDate>2018-12-13T00:00:00+04:00</ns2:documentDate>
    <ns2:documentNumber>1544698824</ns2:documentNumber>
    <ns2:partnerAccountNumber>GE44TB7142536020100005</ns2:partnerAccountNumber>
    <ns2:partnerName>კლიენტი:1425</ns2:partnerName>
    <ns2:partnerTaxCode>123456786</ns2:partnerTaxCode>
    <ns2:partnerBankCode>TBCBGE22</ns2:partnerBankCode>
    <ns2:partnerBank>სს  თიბისი  ბანკი</ns2:partnerBank>
    <ns2:taxpayerCode>123456786</ns2:taxpayerCode>
    <ns2:taxpayerName>კლიენტი:1425, 123456786</ns2:taxpayerName>
    <ns2:operationCode>GII</ns2:operationCode>
    <ns2:partnerDocumentType>0</ns2:partnerDocumentType>
    <ns2:statusCode>3</ns2:statusCode>
    <ns2:transactionType>1</ns2:transactionType></ns2:accountMovement>
    <ns2:accountMovement><ns2:movementId>002147575329.2</ns2:movementId>
    <ns2:externalPaymentId>2147575329</ns2:externalPaymentId>
    <ns2:debitCredit>1</ns2:debitCredit>
    <ns2:valueDate>2018-12-13T00:00:00+04:00</ns2:valueDate>
    <ns2:description>ანგარიშზე თანხის შეტანა</ns2:description><ns2:amount>
    <ns2:amount>100000</ns2:amount><ns2:currency>GEL</ns2:currency></ns2:amount>
    <ns2:accountNumber>GE17TB7142536020100006</ns2:accountNumber>
    <ns2:accountName>კლიენტი:1425</ns2:accountName>
    <ns2:additionalInformation>სახელი:3928462 გვარი3928462</ns2:additionalInformation>
    <ns2:documentDate>2018-12-13T00:00:00+04:00</ns2:documentDate>
    <ns2:documentNumber>63946130</ns2:documentNumber>
    <ns2:partnerAccountNumber>GE78TB0000000000060000</ns2:partnerAccountNumber>
    <ns2:partnerName>სახელი:3928462 გვარი3928462</ns2:partnerName>
    <ns2:partnerTaxCode>00000000000</ns2:partnerTaxCode>
    <ns2:partnerBankCode>TBCBGE22</ns2:partnerBankCode><ns2:partnerBank>ს.ს. „თიბისი ბანკი”</ns2:partnerBank>
    <ns2:operationCode>01</ns2:operationCode>
    <ns2:partnerPersonalNumber>00000000000</ns2:partnerPersonalNumber>
    <ns2:partnerDocumentType>6</ns2:partnerDocumentType>
    <ns2:partnerDocumentNumber>DC102086</ns2:partnerDocumentNumber>
    <ns2:statusCode>3</ns2:statusCode>
    <ns2:transactionType>20</ns2:transactionType>
</ns2:accountMovement>
</ns2:GetAccountMovementsResponseIo>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I would like to get data inside tags one by one <ns2:paymentId>, <ns2:externalPaymentId>, <ns2:debitCredit> etc. 我想在标签内分别获取<ns2:paymentId>, <ns2:externalPaymentId>, <ns2:debitCredit>等数据。

Kindly note that this is dummy data of a bank statement, and in real life responses have multiple tags with the same name per each transaction. 请注意,这是银行对帐单的伪数据,在现实生活中,每个交易的响应都具有多个名称相同的标签。 For example, if there are 11 transactions, there will be 11 <ns2:paymentId> tags and I need to get data one by one. 例如,如果有11个交易,则将有11个<ns2:paymentId>标记,我需要一个一个地获取数据。

Using SimpleXML you can read the code and the data much easier. 使用SimpleXML,您可以更轻松地读取代码和数据。 The only thing is that you need to respect the namespaces. 唯一的事情是您需要尊重名称空间。 So first register the ns2 namespace so that you can then you can fetch the <ns2:accountMovement> elements. 因此,首先注册ns2命名空间,以便随后可以获取<ns2:accountMovement>元素。 The loop over these, but to access the child elements in the namespace, use children("http://www.mygemini.com/schemas/mygemini") to get them into the $data variable, then each access is via this (ie $data->paymentId )... 遍历这些变量,但要访问名称空间中的子元素,请使用children("http://www.mygemini.com/schemas/mygemini")将它们放入$data变量,然后每次访问都通过此(即$data->paymentId )...

$xml = simplexml_load_string($xmlContent);
$xml->registerXPathNamespace("ns2", "http://www.mygemini.com/schemas/mygemini");
$movements = $xml->xpath("//ns2:GetAccountMovementsResponseIo/ns2:accountMovement");
foreach ( $movements as $accMove ) {
    $data = $accMove->children("http://www.mygemini.com/schemas/mygemini");
    echo "paymentId ->".$data->paymentId.PHP_EOL;
    echo "externalPaymentId ->".$data->externalPaymentId.PHP_EOL;
    echo "debitCredit ->".$data->debitCredit.PHP_EOL;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM