SOFuser123
SOFuser123

Reputation: 105

Unable to output xml using cURL PHP

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.

Upvotes: 0

Views: 874

Answers (2)

akirk
akirk

Reputation: 6837

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");

Upvotes: 1

mark.sagikazar
mark.sagikazar

Reputation: 1042

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.

Upvotes: 0

Related Questions