How to force XML response using PHP CURL

Just A Guy

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
Marc B

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>';

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get response from server using curl in php

From Dev

PHP cURL response as XML is shown as plain string

From Dev

How to send XML soap request using php curl

From Dev

How to use curl with proxy and with japanese xml response

From Dev

Read xml response using php

From Dev

returning a XML response with CURL

From Dev

How to encode plain text response to JSON and take specific Data using PHP CURL

From Dev

Send XML data to webservice using php curl

From Dev

Make PHP Request to tomcat localhost using cURL and get response

From Dev

How to access the data in this PHP XML response?

From Dev

How to read soap response xml in php

From Dev

how to send an array as xml response in php

From Dev

How to read soap response xml in php

From Dev

How to convert XML SOAP response to php string

From Dev

How to get xml response by openin url in PHP

From Dev

Curl - how to handle the response

From Dev

How to extract xml response in php and create json response

From Dev

How to Read XML Response using Java

From Dev

Response from Google is different when using PHP curl vs. curl from command line

From Java

How do I measure request and response times at once using cURL?

From Dev

In Jenkins pipeline using curl how can store response cookies?

From Dev

PHP cURL with XML Not Working

From Dev

PHP curl() Response to Associative Array

From Dev

PHP curl Response string splitting

From Dev

Dealing with large curl response - PHP

From Dev

PHP cURL - PARSE server response

From Dev

php curl response blank randomly

From Dev

Blank Response on a cURL Request PHP

From Dev

How to interact with XBox API using PHP and cURL

Related Related

HotTag

Archive