1

I am trying to parse the xml response returned by url. I can successfully view the response but when I try to get the specific value from the xml, then I get nothing. Here is my PHP code.

$responseMessage = file_get_contents($myurl);
$xml   = simplexml_load_string($responseMessage, 'SimpleXMLElement', LIBXML_NOCDATA);
print_r($xml)

Here's the actual xml response

<PPResponse Result="000" Key="99fd1c21-b07d-41f5-bbf8-1917f53c3152">

    <ResultMessage>
        Operation is succesfully completed
    </ResultMessage>

    <UtilityInfo>
            <UtilityCode>78</UtilityCode>
    </UtilityInfo>


        <BillInfo>  
            <Bill>
                <BillNumber>99fd1c21-b07d-41f5-bbf8-1917f53c3152</BillNumber>
                <DueDate>2015-08-06T11:23:49</DueDate>
                <Amount>0</Amount>
                <ReserveInfo>Some info</ReserveInfo>

                <BillParam>
                    <mask>3</mask>
                    <commission type="0" val="0.00" op="-" paysource="1" />
                </BillParam>

                <RefStan>7676422901773</RefStan>
            </Bill>

        </BillInfo>

</PPResponse>

Now I want to save only BillNumber value and RefStan Value into some variable so that I can use it later. How can I do So?

0

2 Answers 2

1

You want to get attribute val from commission property? Try something like this:

$value = (string)$xml->BillInfo->Bill->BillParam->commission->attributes()->val;
6
  • It says 'Trying to get property of non-object'
    – Sid
    Aug 6, 2015 at 8:42
  • I checked this code with exact data that you posted and it is working. So you should be testing it on some data with different structure.
    – Jan.J
    Aug 6, 2015 at 8:44
  • what does that (string) refers to?
    – Sid
    Aug 6, 2015 at 8:48
  • It just converts SimpleXMLElement holding attribute value to string.
    – Jan.J
    Aug 6, 2015 at 8:49
  • worked finally. tons of thanks :)
    – Sid
    Aug 6, 2015 at 9:03
0

Try

$str = <<<XML
<PPResponse Result="000" Key="99fd1c21-b07d-41f5-bbf8-1917f53c3152">

    <ResultMessage>
        Operation is succesfully completed
    </ResultMessage>

    <UtilityInfo>
            <UtilityCode>78</UtilityCode>
    </UtilityInfo>


        <BillInfo>  
            <Bill>
                <BillNumber>99fd1c21-b07d-41f5-bbf8-1917f53c3152</BillNumber>
                <DueDate>2015-08-06T11:23:49</DueDate>
                <Amount>0</Amount>
                <ReserveInfo>Some info</ReserveInfo>

                <BillParam>
                    <mask>3</mask>
                    <commission type="0" val="0.00" op="-" paysource="1" />
                </BillParam>

                <RefStan>7676422901773</RefStan>
            </Bill>

        </BillInfo>

</PPResponse>
XML;

$xml = new SimpleXMLElement($str);
echo $xml->ResultMessage;
echo $xml->BillInfo->Bill->ReserveInfo;

Note Please replace $str with $responseMessage.

Not the answer you're looking for? Browse other questions tagged or ask your own question.