0

I am trying to test cURL option in PHP. But I am unable to print the XML output on the browser. I get parse error. For some reason the beginning of XML is truncated and hence the syntax error. My client code is as follows:

<?php
    header('Content-type: text/xml');
    /**
     * Define POST URL and also payload
     */
    $xml_data = "<?xml version='1.0'?><member><name>XYZ</name><address>1111 yonge street Toronto Canada</address><businessunit>Hotel </businessunit></member>"; 

    define('XML_PAYLOAD',$xml_data);
    define('XML_POST_URL', 'http://localhost:8888/PlatformX/build_xml.php');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 4);
    curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
    //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));

    /**
     * Execute the request and also time the transaction
     */
    $start = array_sum(explode(' ', microtime()));
    $retValue = curl_exec($ch);
    $stop = array_sum(explode(' ', microtime()));
    $totalTime = $stop - $start;

    /**
     * Check for errors
     */

    if ( curl_errno($ch) ) {
        $result = 'ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
    } else {
        $returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
        switch($returnCode){
            case 404:
                $result = 'ERROR -> 404 Not Found';
                break;
            default:
                 $result = 'Success';
                break;
        }
    }

    /**
     * Close the handle
     */
    curl_close($ch);

    /**
     * Output the results and time
     */
    echo 'Total time for request: ' . $totalTime . "\n";
    echo $result; 
    print_r($retValue);     

    /**
     * Exit the script
     */
    exit(0);
?>

And my serer side code is:

<?php
header('Content-type: text/xml');

foreach( $_POST as $xmlstr ) {

echo $xmlstr;

} 
?>

But I am unable to print the XML on the browser.

Request your help. I get the following in response

'1.0'?>XYZ
1111 yonge street Toronto Canada
Hotel 

I am not getting the tag names and if I use header('Content-type: text/xml'); I get parse error.

2 Answers 2

1

You are telling PHP to try and parse the XML string as variables. But rather you want to use the whole input. So change your server side code to this:

<?php
header('Content-type: text/xml');

echo file_get_contents("php://input");
Sign up to request clarification or add additional context in comments.

2 Comments

I changed content type to text/plain ...now im getting "'1.0' encoding='UTF-8'?><member><name>XYZ</name><address>1111 yonge street Toronto Canada</address><businessunit>Hotel </businessunit></member> " For some reason i dont see <?xml version= getting displayed.It only shows starting from '1.0'
What about you change your server-side code to echo file_get_contents("php://input"); instead of the foreach-loop? I have changed my answer accordingly.
0

Make sure your XML is valid. (Put it to a file for example) It should start with <?xml version="1.0"?> Optionally it can include <?xml version="1.0" encoding="UTF-8"?> encoding, which is strongly recommended (Parse error means, that XML is not valid, or encoding cannot be determined)

Also, try to use header('Content-type: application/xml'); instead.

4 Comments

The XML seems to be valid, I think he just wants to display it in the browser for a start.
Sending a header and echoing the content means to me that you want to display it in the browser. Do you want to display the XML in the browser, or you want to display it as text? If you want to do the last one, then why did you send xml headers?
I want to display the xml on the browser.I changed by code to include encoding="UTF-8" also changed content type header('Content-type: application/xml'); Still no luck. Here is the output=>'1.0' encoding='UTF-8'?><member><name>XYZ</name><address>1111 yonge street Toronto Canada</address><businessunit>Hotel </businessunit></member>
Try this: $xml_data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<member><name>XYZ</name><address>1111 yonge street Toronto Canada</address><businessunit>Hotel </businessunit></member>"; Also, just for sure, send utf8 headers: header('Content-Type: application/xml; charset=utf-8');

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.