enriqg9
enriqg9

Reputation: 1507

XML Premature end of data

when running the code below I get the following errors:

Failed loading XML Premature end of data in tag Request line 2

Fatal error: Call to a member function asXml() on a non-object in /home4/viptrav3/public_html/wp-content/themes/voyage-child/transfer.php on line 61

Line 61 is $requestFile->asXml('trequest.xml');

But I don't see the premature closing of the XML. I want to save this as a XML file trequest.xml

//Build XML Request
                $requestData = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
                $requestData .= '<Request>';

                // Create Request Header
                $requestData .= '<Source>';

                // Add Requestor ID data
                $requestData .= '<RequestorID Client="' . $clientID . '" EMailAddress="' . $email . '" Password="' . $password . '" />';

                // Add Requestor Preferences data
                $requestData .= '<RequestorPreferences Language="' . $language . '" Currency="USD" Country="US" >';
                $requestData .= '<RequestMode>' . $requestMode . '</RequestMode>';
                $requestData .= '</RequestorPreferences>';
                $requestData .= '</Source>';

                // Create Request Body
                $requestData .= '<RequestDetails>
                                <SearchTransferPriceRequest>
                                <TransferPickUp>
                                <PickUpCityCode>LON</PickUpCityCode>
                                <PickUpCode>A</PickUpCode>
                                <PickUpPointCode>LCY</PickUpPointCode>
                                </TransferPickUp>
                                <TransferDropOff>
                                <DropOffCityCode>EDI</DropOffCityCode>
                                <DropOffCode>H</DropOffCode>
                                </TransferDropOff>
                                <TransferDate>
                                2013-11-11</TransferDate>
                                <NumberOfPassengers>2</NumberOfPassengers>
                                <PreferredLanguage>E</PreferredLanguage>
                                </SearchTransferPriceRequest>
                                </RequestDetails>';                    
//Save Request & Display XML errors

                libxml_use_internal_errors(true);
                $sxe = simplexml_load_string($requestData);
                if ($sxe === false) {
                    echo "Failed loading XML\n";
                    foreach(libxml_get_errors() as $error) {
                        echo "\t", $error->message;
                    }
                }


                $requestFile = simplexml_load_string($requestData);
                $requestFile->asXml('trequest.xml');

Upvotes: 1

Views: 4117

Answers (1)

Elon Than
Elon Than

Reputation: 9765

You have to close <Request> tag at the end.

Also it's not good to build XML by hand. Use DOMDocument class instead.

Upvotes: 2

Related Questions