1

I am integrating a payment gateway into my website via an API provided by a third party, and I DID succeed. When it processes the payment and returns a successful result in the so-called success.php page, I receive the already-output xml from their simplexml_load_string($result), which is printed out in the success.php page like this.

SimpleXMLElement Object
(
[error_code] => 00
[token] => 1182263526325de72aw828369e7aa
[description] => SimpleXMLElement Object
    (
    )

[transaction_status] => 00
[receiver_email] => [email protected]
[order_code] => 3212423
[total_amount] => 200
[payment_method] => SimpleXMLElement Object
    (
    )

[bank_code] => NLAZ
[payment_type] => 1
[order_description] => SimpleXMLElement Object
    (
    )

[tax_amount] => 0
[discount_amount] => 0
[fee_shipping] => 0
[return_url] => success.php
[cancel_url] => order.php
[buyer_fullname] => eric phuong
[buyer_email] => [email protected]
[buyer_mobile] => 012108609
[buyer_address] => abc
[affiliate_code] => SimpleXMLElement Object
    (
    )

[transaction_id] => 12345
)

What I am trying to do are:

  1. Put the output result above into a variable so that I can get the values. How can I do that in a php and with .php? should I put it like this or how?:

    < ?php $variable = <<< XML SimpleXMLElement Object ( [error_code] => 00 .... XML; ?>

  2. Try to get ALL the values such as the buyer_mobile, buyer_email, etc which are 012108609, [email protected], etc.

Can you help?

Thanks,

Eric

P/S: I also tried to read this entry https://stackoverflow.com/questions/25522047/php-xml-to-array-object-with-attributes-and-values and applied it, but no success.

Note: I already know how to parse the xml file to get the array value, but the process here in this situation is quite different that it is already output and echo'ed out in the success.php page. I TRY TO GET THE VALUES, but it seems to be out of my knowledge. If the question is not clear enough to you, I can update it as per request.

1 Answer 1

0

Your can get the SimpleXMLElement from the return of simplexml_load_string($result)

Then you can access the properties and cast them to a string because the SimpleXMLElement has a __toString method.

$variable = simplexml_load_string($result);
$buyerMobile = (string)$variable->buyer_mobile;
$buyerEmail = (string)$variable->buyer_email;

If you want to get the values from that response you might use a regex with an alternation and use \K to reset the starting point of the reported match:

\[buyer_(?:email|mobile|fullname|address)\] =>\s*\K.*

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

it works perfectly. The point is that I have to read their returned $variable and parse it right from the success.php page. If I copied the output as shown above and saved it in a completely new .php page, I couldn't parse it. Thanks
Thanks a lot @the fourth bird

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.