2

I am testing an XML API which is successfully hitting my account and I am getting a response, but the response appears to be a plain text string with no tags or anything, not even name/pair values. I need the response to be captured as XML so I can use the XML tags to identify the response variables.

I use a form POST to communicate the data from the form to the CURL page which parses the POST into XML format then uses CURL to send it to the API. The API is XML based an I expect an XML response, but instead receive

HELPS!

CURL REQUEST/RESPONSE PAGE

                        <?php
                    if ($_SERVER['REQUEST_METHOD'] == 'POST') 
                    {
                        $xml = "<?xml version='1.0' encoding='UTF-8'?>";
                        $xml .= "<request>";

                        foreach ($_POST as $key => $val) {
                            $xml .= "<".$key.">". $val ."</".$key.">";
                        }
                        $xml .= "<username>user1234</username>";
                        $xml .= "<password>pass1234</password>";
                        $xml .= "</request>";               

                        echo "<br/>" . $xml . "<br/><br/>";

                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL, 'https://www.beanstream.com/scripts/process_transaction.aspx');
                        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
                        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
                        curl_setopt($ch, CURLOPT_HEADER, 1);
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
                        curl_setopt($ch, CURLOPT_POST, 1);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);

                        $output = curl_exec($ch);
                        curl_close($ch);

                        print_r($output);       
                    }
                    ?>

API RESPONSE with header

HTTP/1.1 200 OK Cache-Control: private Content-Length: 724 Content-Type: application/xml; charset=utf-8 X-Powered-By: ASP.NET X-AspNet-Version: 2.0.50727 Set-Cookie: ASP.NET_SessionId=1p4jp2uoz2de3f45iolold3j; path=/; HttpOnly Date: Wed, 16 Oct 2013 21:39:38 GMT 1100001211ApprovedQCE9HW5ETESTNT53.4810/16/2013 2:39:38 PM1W001Postal/ZIP matches, street address does not match.1VIPCC7
3
  • 1
    It returns fine. Don't look at it in a browser, look at the source of the page (rendering as html will hide all the tags, and newlines, etc.) Commented Oct 16, 2013 at 21:50
  • You are correct, I can see the XML format in the page source. How do I display the response as an XML tree on screen? Is there anyway easy way to do this without javascript? Commented Nov 1, 2013 at 16:58
  • Anyone know how I can display the XML on screen as an XML tree, similar to if you were opening an XML document in the web browser? Commented Nov 8, 2013 at 23:08

3 Answers 3

11

You're just spitting out the result, probably into a browser. That browser is probably trying to render the XML tags as HTML, and failing because they're not real html. Unknown tags are simply NOT rendered.

Try a "view source" and you'll see your xml, or do

echo '<pre>';
echo htmlspecialchars(print_r($output, true));
echo '</pre>';
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a million times and +1. I was spending entire day to figure this thing out.
1

Alternatively to what Marc B said, you can also forward the Content-Type in order to make your browser display the XML document the same way it would if you were acessing it directly.

You can achieve that by doing this:

header('Content-Type: application/xml; charset=utf-8');
echo $output;

Comments

1

I agree with the @Marc B's answer. It is what worked for me.

simplexml_load_string didnt do what I wished.

I found it tedious to have to loop through each name of the tag and get the XML Object to show all of its contents.

After some more research:

echo htmlspecialchars(print_r($response, true));

with htmlspecialchars you can translate your Curl xml response to XHTML.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.