0

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).

2
  • 1
    There is nothing wrong with the response, you are just displaying it as HTML - which is why when you inspect the page it has the tags in there. What is the problem you think your having? Commented Mar 7, 2018 at 15:33
  • 3
    Your browser is interpreting the direct url as xml and formatting it for a human to read. In PHP its grabbing the content into a string and outputting it with default headers. You need to use a library or class to turn the string into an XML Object to manipulate. OR set the output header('Content-Type: text/xml'); before output to the browser. Commented Mar 7, 2018 at 15:33

2 Answers 2

3

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 ;
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

4 Comments

Hmm, question. The data I send to parser should already be XML formatted, at the moment I don't have such data. All the response I am getting is a plain string, like this: prnt.sc/io4pnl
@The50 That's your browser interpreting the string as HTML, and your browser won't be involved when you're passing it to the parser. If it looks right when you View Source, then the string itself is fine.
@The50 Please re-read the information provided here. Stop looking at the output in the web browser window.
Thank you, problem solved. Just put the response data directly to parser and got the object with all the values. ( $parse = new SimpleXMLElement($data); )

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.