T
T
TERRAN472017-01-26 13:25:56
XML
TERRAN47, 2017-01-26 13:25:56

Parsing XML in PHP to find key value?

Good day! the situation is as follows: I set up a bank payment via a card, after a successful payment, the bank sends XML where I need to take the order_id, how to parse it (PHP) and take the data that is entered into the data base. I write in laravel
Here is what comes up:

<document>
<bank name="Kazkommertsbank JSC">
<customer name="TERRA" mail="[email protected]" phone="">
<merchant cert_id="00c15670b" name="ZARA’S GROUP">
<order order_id="374565664" amount="8000" currency="398">
<department merchant_id="92451103" amount="8000"/>
</order></merchant><merchant_sign type="RSA"/>
</customer><customer_sign type="RSA"/>
<results timestamp="2017-01-26 15:21:21">
<payment merchant_id="9256е1103" card="548318-XX-XXXX-0293" amount="8000" reference="17012562121" approval_code="152121" response_code="00" Secure="No" card_bin="" c_hash="D643983890D0003EA973E88A346CDDBE"/></results>
</bank>
<bank_sign cert_id="00c154690" type="SHA/RSA">k72PKDHhQ2QMyi8eGH7SRf4+4q4h3TTjTS4x+XIIpubT/kR67yTbKSYYRFg6iUv6NROx6SCm+hVJoaztBk/2QVU47FlIFe60iMVliONSIZ5RvLvYZwy/2q2b7vLiav8ZKOFahLb7NfhoBrNJeNKUFEXYn3EKKx2ojiMj7jWr/Q8=</bank_sign></document>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Osher, 2017-01-26
@miraage

// EDIT
PHP 7.1.0

<?php

$data = <<<XML
<document>
  <bank name="Kazkommertsbank JSC">
    <customer name="TERRA" mail="[email protected]" phone="">
      <merchant cert_id="00c15670b" name="ZARA’S GROUP">
        <order order_id="374565664" amount="8000" currency="398">
          <department merchant_id="92451103" amount="8000"/>
        </order>
      </merchant>
      <merchant_sign type="RSA"/>
    </customer>
    <customer_sign type="RSA"/>
      <results timestamp="2017-01-26 15:21:21">
        <payment merchant_id="9256е1103" card="548318-XX-XXXX-0293" amount="8000" reference="17012562121" approval_code="152121" response_code="00" Secure="No" card_bin="" c_hash="D643983890D0003EA973E88A346CDDBE"/>
      </results>
  </bank>
  <bank_sign cert_id="00c154690" type="SHA/RSA">
    k72PKDHhQ2QMyi8eGH7SRf4+4q4h3TTjTS4x+XIIpubT/kR67yTbKSYYRFg6iUv6NROx6SCm+hVJoaztBk/2QVU47FlIFe60iMVliONSIZ5RvLvYZwy/2q2b7vLiav8ZKOFahLb7NfhoBrNJeNKUFEXYn3EKKx2ojiMj7jWr/Q8=
  </bank_sign>
</document>
XML;

$xml = new SimpleXMLElement($data);

var_dump((int) $xml->xpath('//order')[0]->attributes()['order_id']);

int(374565664)

S
Stanislav Pochepko, 2017-01-26
@DJZT

Let me google for you
https://www.google.com.ua/webhp?sourceid=chrome-in...
First link
php.net/manual/ru/simplexml.examples-basic.php

<?php
include 'example.php';

$xml = new SimpleXMLElement($xmlstr);

echo $xml->document->bank_sign;
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question