The50
The50

Reputation: 1166

PHP cURL response as XML is shown as plain string

if I run this link on my browser, I get the proper XML formatted response: http://old.lb.lt/webservices/FxRates/FxRates.asmx/getCurrentFxRates?tp=EU

Now if I am running a POST request for the same link using cURL and when I dump the response I get only a string. If I make

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

Then the response is shown in my browser without XML tags, but if I inspect the page I see that there is all the XML tags and it looks good.

What could I do to parse this valid response with all the XML tags to variable?

Also I set the response to be:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/x-www-form-urlencoded'
));

As stated in documentation here http://www.lb.lt/webservices/FxRates/FxRates.asmx?op=getCurrentFxRates (at the very bottom of the page).

Upvotes: 0

Views: 3155

Answers (2)

IMSoP
IMSoP

Reputation: 97648

when I dump the response I get only a string

Yes, the output of anything over HTTP is a string of bytes. In this case, that string is the content of the XML document.

Then the response is shown in my browser without XML tags

That's because PHP by default tells the browser to interpret its output as HTML; when the browser interprets XML as HTML, it simply ignores all the unrecognised tags, and outputs anything in between them.

You could encode the XML inside an HTML document using htmlspecialchars(), or instruct the browser to interpret it as XML by setting a content type header:

header('Content-Type: text/xml');

if I inspect the page I see that there is all the XML tags and it looks good.

The "View Source" view or DOM Inspector in your browser is still interpreting the content as HTML, but is intended to show all the tags (recognised ones like <div> and <p>, and unrecognised ones like <some-xml-tag>).

What could I do to parse this valid response with all the XML tags to variable?

Well, that depends what kind of variable you want it in; the string you have is a variable with the XML in it already, if we want to take the question literally.

Most likely what you're looking for is an XML parser that lets you get some data out of the XML and do something with it in PHP, the simplest parser is appropriately called SimpleXML, and you can find basic examples for using it in the PHP manual.

(Please don't be tempted to convert the whole XML document into an array - XML's structure doesn't fit well into a simple array, and SimpleXML is designed to cope with that complexity nicely for you; it does a good job, once you get used to it.)

Also I set the response to be:

curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded' ));

This isn't setting anything to do with the response; it's sending an HTTP header to the API specifying the format of the data that you're sending them; specifically, that you're sending it in the format a browser would use if you submitted an HTML form.

Upvotes: 2

Syscall
Syscall

Reputation: 19780

You could use simplexml_load_string() to convert your string into an XML object :

$xmlstring = curl_exec($ch); 
$xml = simplexml_load_string($xmlstring);
foreach ($xml->FxRate as $rate) {
   echo (string)$rate->Tp ;
}

Upvotes: 3

Related Questions