Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

Could you point a way to transverse this SoapVar Object containing a XML by using PHP ?

Hi Experts

Could you point a way to transverse this SoapVar Object by using PHP to obtain the values expressed by
pdf_coupon_title
pdf_promotionid
pdf_barcode ?

SoapVar Object
(
    [enc_type] => 147
    [enc_value] => <payment_request_response_code value="9230">
    <transaction_uid value="93163"/>
    <date value="20210121"/>
    <hour value="10:29:07"/>
    <number_of_coupons value="0"/>
    <coupons>
    </coupons>
    <store_number value="0104"/>
    <ptv_number value="0038"/>
    <point_balance value="0"/>
<point_unusable value="0"/>
<city_code value=""/>
<pdf_coupons>
<pdf_coupon pdf_coupon_title="Teste" pdf_promotionid="14" pdf_barcode="29600104210121000131" pdf_ean_type="EAN128" pdf_amount="29.67" pdf_text="VGVzdGUKCnRlc3RlCnRlc3RlCgpDVVBPTSBWQUxJRE8gREU6IDIwLzAxLzIwMjEgQVRFIDIwLzAx LzIwMjIKRU0gVE9EQVMgQVMgTE9KQVMKCiAgICAgICAgICAgICAgICAgICAgIHRlc3RlCiAgICAg ICAgICAgICAgICAgICAgIHRlc3RlCiAgICAgICAgICAgICAgICAgICAgIHRlc3RlCiAgICAgICAg ICAgICAgICAgIC AgIHRlc3RlCiAgICAgICAgICAgICAgICAgICAgIHRlc3RlCiAgICAgICAgICAg ICAgICAgICAgIHRlc3RlCiAgICAgICAgICAgICAgICAgICAgIHRlc3RlCiAgICAgICAgICAgICAg ICAgICAgIHRlc3RlCiAgICAgICAgICAgICAgICAgICAgIHRlc3RlCiAgICAgICAgICAgICAgICAg ICAgIHRlc3RlCiAgICAgICiAx NAo="/>
</pdf_coupons>
<management_coupon>
CiAgICAgICAgICAgICAgICAgICAgUFJPTU9DQU8gICAgICAgICAgICAgICAgICAgIAoKUFJPTU9D
QU8gQ09OQ0VESURBIE5PUyBQUk9EVVRPUyBBQkFJWE8gOgoKSVRFTSBDT0RJR08gREVTQ1J
ICAgICAgICAgICAgICAgICAVEVNIFIkClBST01PQ0FPIG9mZmVyXzFfY2xhc3NpY19sb3Qg
ICAgICAgICAgICAgIC0yOC43
OQogIDAwNCA3NjkzNzQ1ICAgICAgICAgICAgICAgICAgICAgICAg
ICAgICAgLTcuMjAKICAwMDUgNzY5Mzc1MyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC03
LjIwCiAgMDA2IDc2OTM3NjEgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC0xNC4zOQ==
</management_coupon>
</payment_request_response_code>
)

Open in new window

Another thing needed is to determine if the XML contains or not <pdf_coupon>

Thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Eduardo Fuerte

ASKER

Hi gr8gonzo

We always have one or none  <pdf_coupon> entries
The code should work for that scenario.
Ok.

Not clear for me:

$xml = $x->enc_value; // <-- Your raw XML string

Open in new window


enc_value is the name of the node, ok?


That error occurs:
SimpleXMLElement::__construct() expects parameter 1 to be string, object given

$response = new SimpleXMLElement($x);
SoapVar is just a little helper/wrapper class that helps you construct the XML used within a SOAP communication. The "enc_value" property contains the raw XML inside of it:
User generated image
Runs....

SimpleXMLElement Object
(
    [0] => Teste
)

SimpleXMLElement Object
(
    [0] => 14
)

SimpleXMLElement Object
(
    [0] => 29600104210121000177
)

Open in new window

Just one final thing....

To obtain just the string value I tried

$pdf_coupon["pdf_coupon_title"][0]

Open in new window


And
$pdf_coupon["pdf_coupon_title"]->enc_value

Open in new window


Also

(array) $pdf_coupon["pdf_coupon_title"]

Array
(
    [0] => Teste
)

Array
(
    [0] => 14
)

Array
(
    [0] => 29600104210121000201
)

Open in new window


No results...
How could I do that ?
Sorry, I should have mentioned that in my initial response. Just add (string) before it:

So instead of something like:
$title = $pdf_coupon["pdf_coupon_title"];

...do this instead:
$title = (string)$pdf_coupon["pdf_coupon_title"];

SimpleXml has some unusual behavior with some things. Basically, it wants to handle everything with classes, but when you add (string) to the beginning, you're telling it, "I want the value of this element / attribute as a string, not as an object!" It's a process called "casting".
Perfect !!!

Teste
14
29600104210121000238

Open in new window

gr8gonzo

Thank you for so good solution!!!