Gabriel Santos
Gabriel Santos

Reputation: 4974

XML and & symbol

I have a question about "&" symbol:

I have an return url in this format:

http://localhost/index.php?task=payment_return&id=113&token=750ac376e9ee1f6e87eba80c409005c4

I set it to XML in my PHP function, inside "url-retorno":

private function XMLUrlRetorno() {
    // $msg = '<url-retorno>' . $this->getData('url_retorno') . '</url-retorno>';
    $msg = '<url-retorno>' . str_replace('&', '&amp;', $this->getData('url_retorno')) . '</url-retorno>';
    return $msg;
}

Ok, but the problem is the "&", if my url are written in this format:

http://localhost/index.php?task=payment_return&id=113&token=750ac376e9ee1f6e87eba80c409005c4

Or

http://localhost/index.php?task=payment_return&amp;id=113&amp;token=750ac376e9ee1f6e87eba80c409005c4

I get this error from XML:

error: Unexpected end of file after url-retorno

I have tried with htmlspecialchar, with str_replace('&', ..) and again error..

What to do, if 'url-retorno' have not "&" it's working propertly, but if have, not?

The XML send:

<?xml version="1.0" encoding="ISO-8859-1"?>
<requisicao-transacao id="c1303587fb566fdec7ae480f5b8d3e04" versao="1.1.0">
  <dados-ec>
    <numero>0000000000</numero>
    <chave>a00000000a0000000000a0000000aa0000a0000000000a0aaa0a0000aa0aa000</chave>
  </dados-ec>
  <dados-pedido>
    <numero>185</numero>
    <valor>15</valor>
    <moeda>986</moeda>
    <data-hora>2011-11-20T13:57:51</data-hora>
    <descricao>185 - teste: 1 x $15.00</descricao>
    <idioma>PT</idioma>
  </dados-pedido>
  <forma-pagamento>
    <bandeira>visa</bandeira>
    <produto>1</produto>
    <parcelas>1</parcelas>
  </forma-pagamento>
  <url-retorno>http://localhost/index.php?task=payment_return&amp;id=185&amp;token=4babfe87206d14cc88220810d44bbb28</url-retorno>
  <autorizar>3</autorizar>
  <capturar>1</capturar>
</requisicao-transacao>

The XML response:

<?xml version="1.0" encoding="ISO-8859-1"?>
<erro xmlns="http://ecommerce.cbmp.com.br">
  <codigo>001</codigo>
  <mensagem>XML inválido: 'error: Unexpected end of file after url-retorno'.</mensagem
</erro>

Upvotes: 0

Views: 442

Answers (2)

Frederick Behrends
Frederick Behrends

Reputation: 3095

Just use the cdata tag in your xml

 <![CDATA[
       Your url with & in it
     ]]>

This should look like this in your code and is totally xml conform.

    private function XMLUrlRetorno() {
        $msg = '<url-retorno><![CDATA[' . $this->getData('url_retorno') . ']]></url-retorno>';
        return $msg;

}

Upvotes: 1

Odys
Odys

Reputation: 9090

There are some characters that are not accepted by xml markup (read this read this link.)

When you have unescaped characters, encode them using htmlspecialchars or the reverse method to escape them.

Upvotes: 2

Related Questions