returning a XML response with CURL

16,943

Your code retrieve full XML, to see it write:

echo htmlentities( $server_output );
die();

and in the browser you'll see:

<?xml version="1.0" encoding="utf-16" ?> <ChameleonIAPIResponse>

<Titles><TitleId>1</TitleId><Title></Title><TitleId>6</TitleId><Title>Mr</Title><TitleId>2</TitleId><Title>Mrs</Title><TitleId>3</TitleId><Title>Miss</Title><TitleId>4</TitleId><Title>Ms</Title><TitleId>5</TitleId><Title>Dr</Title><TitleId>43</TitleId><Title>Sir</Title></Titles> </ChameleonIAPIResponse>

The problem is that the browser interpret your output as HTML, so the tag are hidden (see at the browser page source, and you will find your complete XML).

In addition, to send XML as XML, before you have to send appropriate headers:

header( 'Content-type: text/xml' );
echo $server_output;
die();

No other output before and after above code, otherwise your XML will result broken.

If you prefer SimpleXML, you can do this:

$oXML = new SimpleXMLElement( $server_output );
header( 'Content-type: text/xml' );
echo $oXML->asXML();
die();

Also in this case, no output before and after (So comment previous lines).

Share:
16,943

Related videos on Youtube

Jack Gleeman
Author by

Jack Gleeman

Just some of my languages :D Java - Oh yea love that. Android - Love it, nice and easy. Blackberry - ¬_¬ Objective C - Hard but nice, don't mind it iPhone - Not bad, I like the IDE C++ - Shudder Hard but very good Python - Cool I like it PHP - Very cool, I like it a lot Javascript - meh HTML - Easy :S CSS - meh lol

Updated on June 04, 2022

Comments

  • Jack Gleeman
    Jack Gleeman almost 2 years

    I am passing some parameters via HTTP POST to a webserver.

    I am getting a response, but ideally I want a full XML responses, whereas I seem to be only getting a concatenated string. I've tried SimpleXMLElement, but it doesn't seem to do anything, and it isn't returning any XML.

    Below is my code:

    $post_string = '<?xml version="1.0" encoding="utf-16" ?> 
    <ChameleonIAPI>
      <Method>TitleList</Method> 
      <APIKey>D12E9CF3-F742-47FC-97CB-295F4488C2FA</APIKey> 
      <UserName>David</UserName>
      <Filter>
      </Filter>
    </ChameleonIAPI>';
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL,"https://jobs.chameleoni.com/PostXML/PostXml.aspx");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,
            "Action=postxml&AuthKey=Guest&AuthPassword=KgwLLm7TL6G6&Xml=$post_string");
    
    // receive server response ...
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
    
    $server_output = curl_exec ($ch);
    
    echo $server_output." TEST <br />";
    curl_close ($ch);
    
    $oXML = new SimpleXMLElement($server_output);
    
    foreach($oXML->entry as $oEntry){
      echo $oEntry->title . "\n";
    }
    

    Here's the page which you can even test the POST and XML.

    https://jobs.chameleoni.com/PostXML/PostingXML.html

    My XML seems to be ok, as it works on the test page. I assume there is something wrong with my PHP, although I have no idea what that is!

    Any help would be lovely!

  • Jack Gleeman
    Jack Gleeman about 8 years
    Just edited, but the output was: string(374) " 16234543 " Not the XML format?
  • Jack Gleeman
    Jack Gleeman about 8 years
    Thanks. I could Echo the XML as you said, but when trying to create the XML I got a server 500 error? With this? Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /Applications/MAMP/htdocs/curl2.php:37 Stack trace: #0 /Applications/MAMP/htdocs/curl2.php(37): SimpleXMLElement->__construct('<?xml version="...') #1 {main} thrown in /Applications/MAMP/htdocs/curl2.php on line 37
  • Alex Andrei
    Alex Andrei about 8 years
    make sure to run exactly the code in your initial question and my answer, I just made a quick sample and received correct output. I have no idea where that output can come from.
  • fusion3k
    fusion3k about 8 years
    You have to pass to SimpleXMLElement the $server_output value, not the htmlentities converted. Also it seems that the server output a malformed XML: “Error: Document labelled UTF-16 but has UTF-8” utf-16 is your choice or server default?
  • fusion3k
    fusion3k about 8 years
    No, the problem is in chameleoni.com response, not in your mac
  • fusion3k
    fusion3k about 8 years
    @JackGleeman Dirty solution, but I have tested and it works for me: $server_output = preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $server_output); $oXML = new SimpleXMLElement($server_output); (copied from here). Stay connected, if I found better way, I will post it
  • fusion3k
    fusion3k about 8 years
    I realize that camaleoni.com is you, right? You can't directly change output encoding to utf-8?
  • Jack Gleeman
    Jack Gleeman about 8 years
    Chameleon is not me, it's the API I have to work with :(
  • Jack Gleeman
    Jack Gleeman about 8 years
    That's worked. Dude, you're a legend. Marking your answer as right.