开发者

PHP's XML Output Error: "XML or text declaration not at the start of entity"

开发者 https://www.devze.com 2023-03-12 07:32 出处:网络
I\'m using PHP version > 5.2 (5.2.17 on my web server and 5.3.6 on my localhost) with the following settings:

I'm using PHP version > 5.2 (5.2.17 on my web server and 5.3.6 on my localhost) with the following settings:

output_buffering = On
output_handler = ob_gzhandler

In the following PHP script I'm outputting an XML response to use with jQuery/AJAX

if (empty($_GET))
{
    $Response = new Response('getlanguage');
    $Response->DisplayLanguage(LanguagesManager::GetLanguage());
    die();
}

IOManager::InputSanitizeRequest($_GET);
$Language = isset($_GET['language']) ? $_GET['language'] : '';

$Response = new Response('setlanguage');

if (LanguagesManager::ValidateLanguage($Language))
{
    LanguagesManager::SetLanguage($Language);
    $Response->DisplayResult(true);
}
else
    $Response->DisplayResult(false);

But my jQuery/AJAX function is not working and, if I try to manually check the output of my PHP script, I get the following error on Mozilla Firefox:

XML Parsing Error: XML or text declaration not at the start of entity
Address: http://127.0.0.1/responses/requestlanguage.php?language=it
Line Number 1, Column 2: <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-^

And that's caused by a trailing whitespace before the XML declaration:

" <?xml version="1.0" encoding="UTF-8" standalone=开发者_运维百科"yes"?>"

I've checked out my PHP tags and everything is fine. After messing up a little bit with my code, I discoveded something incredible: the whitespace is created by this function:

LanguagesManager::ValidateLanguage($Language)

public static function ValidateLanguage($Language)
{
    return in_array($Language, self::GetSupportedLanguages());
}

If I comment the whole if statement, or if i replace that function with a simple "true" I don't get the parsing error anymore... well, I just can't belive it. Can someone explain me why? Anyone knows a solution?


If you didn't find the error, you could try to catch the space with output buffering, like this:

ob_start();
// here the code part, which seems to print the whitespace (but nothing else)
$langValidated = LanguagesManager::ValidateLanguage($Language);
ob_end_clean();

if ($langValidated)
{
    LanguagesManager::SetLanguage($Language);
    $Response->DisplayResult(true);
}
else
    $Response->DisplayResult(false);
0

精彩评论

暂无评论...
验证码 换一张
取 消