PHP / CakePHP returning malformed XML

1.1k Views Asked by At

Basically I am trying to add Google Checkout order processing (level 2 integration) into a CakePHP app using the Google Checkout PHP sample code.

I can successfully create carts and receive notifications sent from Google to my app, however it cannot acknowledge these properly.

The function in google's code to do this echo's this:

<?xml version="1.0" encoding="UTF-8" ?><notification-acknowledgment xmlns="http://checkout.google.com/schema/2" serial-number="119963104284921-00001-7" />

However Google Checkout receives that code like this:

<?xml version=1.0 encoding=UTF-8 ?><notification-acknowledgment xmlns="http://checkout.google.com/schema/2" serial-number=119963104284921-00001-7 />

I can't work out what is causing this, I don't think it has anything to do with Cake and I've disabled PHP short tags so I can use inline XML but this makes no difference.

If I remove the first < from the string, the function echo's the rest of the code correctly, leave it in and it brakes (obviously this also applies for the ? and closing > symbol too, I just cant't have all of them at once!).

I have been able to replicate this myself in attempt to remove any unseen stuff being done in the Google Code using the function below. It does exactly the same thing but is contained within a (Cake) controller and hard codes the variables.

public function blank($tags = null) {
    $schema = 'http://checkout.google.com/schema/2';
    $serial = '119963104284921-00001-7';
    if ($tags != null) {
        $acknowledgment = '<?xml version="1.0" encoding="UTF-8" ?'.'>'.'<notification-acknowledgment xmlns="'.$schema.' '.'serial-number="'.$serial.'" />';
    } else {
        $acknowledgment = '?xml version="1.0" encoding="UTF-8" ?>'.'<notification-acknowledgment xmlns="'.$schema.' '.'serial-number="'.$serial.'" />';
    }
    $this->set('_ack', $acknowledgment);
}

The IF statement is used to show the difference the < symbol makes.

Calling it with nothing ($tags == null) gives this output:

<?xml version="1.0" encoding="UTF-8" ?><notification-acknowledgment xmlns="http://checkout.google.com/schema/2" serial-number="119963104284921-00001-7" />

Source.

Calling it with any other value ($tags != null) gives this output:

<?xml version=1.0 encoding=UTF-8 ?><notification-acknowledgment xmlns="http://checkout.google.com/schema/2" serial-number=119963104284921-00001-7 />

Source.

My question (finally!) is, why does this happen? and how can I get it output the XML correctly?

Sorry if I've missed something really obvious, but I'd rather have it pointed out here than faff around getting nowhere for another day!

3

There are 3 best solutions below

1
On

You forgot to close the quotes on the xmlns attribute, that's the probable cause. Try this:

$acknowledgment = '<?xml version="1.0" encoding="UTF-8" ?'.'>'.'<notification-acknowledgment xmlns="'.$schema.'" '.'serial-number="'.$serial.'" />';
1
On

Some XML parsers do not allow specifying the encoding in capital letters (e.g. UTF-8 is incorrect).

Try changing the encoding to "utf-8" to see if that fixes it.

6
On

this "problem" has to do with the PHP interpreter.

There are solutions but everything in View not in the Controlled.

//var
$questionmark = "?";
echo "<".$questionmark."xml version=\"1.0\" encoding=\"utf-8\"".$questionmark.">";
// separate
echo '<' . '?xml version="1.0" encoding="utf-8"?' . '>';