1

I've fired a curl request. My code is

$URL = "http://demo.com";

            $ch = curl_init($URL);
            curl_setopt($ch, CURLOPT_MUTE, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
            curl_setopt($ch, CURLOPT_POSTFIELDS, "$xmlLeadCloud");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);

            curl_close($ch);

Below I am getting the XML in $output variable.

<?xml version="1.0" encoding="utf-16"?>
<LeadCloud>
    <InsuranceSvcRs>
        <PersHealthPolicyQuoteInqRs>
            <MsgStatus>
                <MsgStatusCd>Success</MsgStatusCd>
                <Payout>17.40</Payout>
                <BuyerLeadId>4ded4da790c6fasasasas322</BuyerLeadId>
                <BuyerId>34</BuyerId>
            </MsgStatus>
        </PersHealthPolicyQuoteInqRs>
    </InsuranceSvcRs>
</LeadCloud>

How could I get MsgStatusCd element's value from it. So far I've tried.

$s = new SimpleXMLElement($output);
echo $s->InsuranceSvcRs->PersHealthPolicyQuoteInqRs->MsgStatus->MsgStatusCd; die; //not working
6
  • and you are doing it right, what is the problem ? Commented Jul 20, 2015 at 15:45
  • It is not showing output $s->InsuranceSvcRs->PersHealthPolicyQuoteInqRs->MsgStatus->MsgStatusCd; In my case it should show Success Commented Jul 20, 2015 at 15:46
  • Do you have error reporting on? Getting message SimpleXMLElement::__construct(): Entity: line 1: parser error : Document labelled UTF-16 but has UTF-8 content in .. Commented Jul 20, 2015 at 15:49
  • 1
    Take a look here, you just need to change encoding to utf-8, and use this for changing. Commented Jul 20, 2015 at 15:50
  • @chris85, yes my reporting is on. Commented Jul 20, 2015 at 15:54

2 Answers 2

2

Cast the output to a string to get its value:

echo (string) $xml->InsuranceSvcRs->PersHealthPolicyQuoteInqRs->MsgStatus->MsgStatusCd;

You can see from this demo where it prints:

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

5 Comments

You should never write a code like this against external API's prone to changes. Code like this will inevitably lead to "trying to get property of non-object" and as a result, you will loose control over your code path introducing exceptional state where there should be none.
@nickb It's not working in my case. Will you please suggest me another way
@Vineet the encoding of the XML is throwing it off. The demo here has altered the encoding of the XML to utf8.
@nickb API output is not in my hand. How could I convert utf-16 to utf-8 ?
@Vineet - You can modify the value from utf-16 to utf-8 as suggested in this thread, but it seems hacky. You might want to consider an alternative than the simplexml parser.
0

You can use XPath to access this data (see PHP manual entry).

The reason why your accessor does not work is that the XML you are receiving is a data array, hence,

<?xml version="1.0" encoding="utf-16"?>
<LeadCloud> <!-- ROOT -->
    <InsuranceSvcRs> <!-- Zero or more -->
        <PersHealthPolicyQuoteInqRs> <!-- Zero or more -->
            <MsgStatus> <!-- Zero or more -->
                <MsgStatusCd>Success</MsgStatusCd> <!-- Zero or one -->
                <Payout>17.40</Payout> <!-- Zero or one -->
                <BuyerLeadId>4ded4da790c6fasasasas322</BuyerLeadId> <!-- Zero or one -->
                <BuyerId>34</BuyerId> <!-- Zero or one -->
            </MsgStatus>
        </PersHealthPolicyQuoteInqRs>
    </InsuranceSvcRs>
</LeadCloud>

and should be accessed as such.

Comments

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.