Jump to content

PHP script works on one host and doesn't work on another


Alex108

Recommended Posts

I have a script that sends text message to a port of another server using POST method.The text is written in some variable that is inside XML and the XML also is inside some variable.So, it looks like this:

$postdataIn = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"...."<data type=\"text\"><![CDATA[".$text."]]></data>".

The text is inside the $text. The text could be in English and in some other languages as well, so CDATA is usedto prevent parsing. It works well if text is in English, but when it is not in English in particular host i receive an error message"020Error parsing XML document: Error on line 1 of document :EOF while parsing <![CDATA[ section. Nested exception: EOF while parsing <![CDATA[ section"The point is that the script works correctly in my localhost and two other web hostings. I tried to spoke with support and even sent them the code, but the answer was:"I'm sorry but I don't understand a word of this"and also "A code that works on another host does not necessarily have to work on all other hosts" :):) Is there the way to solve this problem or i have to change the host?

Link to comment
Share on other sites

- But it works if the language is English?- Could you post a part of the xml in that non-english language which might be making it break?- no do not change host yet, may be there's a work-around..

Link to comment
Share on other sites

- But it works if the language is English?
Yes. And it works also with other languages on my localhost and two other web hostings.
- Could you post a part of the xml in that non-english language which might be making it break?
$text = "Привет";$postdataIn = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".							"<sms command=\"submit\" version=\"1.0\">".								"<account>".									"<id>".$user."</id>".									"<password>".$pass."</password>".								"</account>".																"<data type=\"text\"><![CDATA[".$text."]]></data>".							"</sms>";

Important that this $postdataIn is sent using socket connection to a port, not to a web page.The error message is

020Error parsing XML document: Error on line 1 of document : End of entity not allowed; an end tag is missing. Nested exception: End of entity not allowed; an end tag is missing.
Link to comment
Share on other sites

1) Could you do this and post back your observation: echo mb_detect_encoding($text);(do it after the line: $text = "Привет"; )NOTE: I would prefer you do it from the source which is giving you that text, instead of manually putting $text = "Привет";2) Could you try encoding the $text before outputting it there in the xml?e.g:$text = utf8_encode("Привет");try these two and post back..

Link to comment
Share on other sites

First stop, you don't need to use CDATA.Make sure your PHP file is UTF-8 encoded (in Notepad, go to "File > Save As...", and make sure the "Encoding" dropdown says "UTF-8").After that, instead of using CDATA, assuming the text is allowed to contain special XML characters, escape any special characters and specify the encoding when doing so. While you're at it, you might want to do the same on the rest of the non-hardcoded data, just in case, i.e.

$text = "Привет";$postdataIn = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>". "<sms command=\"submit\" version=\"1.0\">". "<account>". "<id>". htmlspecialchars($user, ENT_NOQUOTES, 'UTF-8') ."</id>". "<password>". htmlspecialchars($pass, ENT_NOQUOTES, 'UTF-8') ."</password>". "</account>". "<data type=\"text\">". htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8') ."</data>". "</sms>";

Don't be fooled by the "html" in the function's name - XML, HTML and XHTML share the same special characters.

Link to comment
Share on other sites

1) Could you do this and post back your observation: echo mb_detect_encoding($text);(do it after the line: $text = "Привет"; )NOTE: I would prefer you do it from the source which is giving you that text, instead of manually putting $text = "Привет";
UTF-8
2) Could you try encoding the $text before outputting it there in the xml?e.g:$text = utf8_encode("Привет");
Didn't help, but error message changed a bit:
020Error parsing XML document: Error on line 1 of document : Expected "</data>" to terminate element starting on line 1. Nested exception: Expected "</data>" to terminate element starting on line 1.
Link to comment
Share on other sites

First stop, you don't need to use CDATA.Make sure your PHP file is UTF-8 encoded (in Notepad, go to "File > Save As...", and make sure the "Encoding" dropdown says "UTF-8").After that, instead of using CDATA, assuming the text is allowed to contain special XML characters, escape any special characters and specify the encoding when doing so. While you're at it, you might want to do the same on the rest of the non-hardcoded data, just in case, i.e.
$text = "Привет";$postdataIn = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>". "<sms command=\"submit\" version=\"1.0\">". "<account>". "<id>". htmlspecialchars($user, ENT_NOQUOTES, 'UTF-8') ."</id>". "<password>". htmlspecialchars($pass, ENT_NOQUOTES, 'UTF-8') ."</password>". "</account>". "<data type=\"text\">". htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8') ."</data>". "</sms>";

Don't be fooled by the "html" in the function's name - XML, HTML and XHTML share the same special characters.

Didn't help
Link to comment
Share on other sites

Let's get some more accurate error messages here... try this (still assuming your PHP file is UTF-8 encoded):

$text = "Привет";$postdataIn = '<?xml version="1.0" encoding="UTF-8"?><sms command="submit" version="1.0"> <account> <id>'. htmlspecialchars($user, ENT_NOQUOTES, 'UTF-8') . '</id> <password>' . htmlspecialchars($pass, ENT_NOQUOTES, 'UTF-8') . '</password> </account>  <data type="text">'. htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8') . '</data> </sms>';

It's probably not going to fix the problem itself, but it's likely to present a more sensible error message.

Link to comment
Share on other sites

Let's get some more accurate error messages here... try this (still assuming your PHP file is UTF-8 encoded):
$text = "Привет";$postdataIn = '<?xml version="1.0" encoding="UTF-8"?><sms command="submit" version="1.0"> <account> <id>'. htmlspecialchars($user, ENT_NOQUOTES, 'UTF-8') . '</id> <password>' . htmlspecialchars($pass, ENT_NOQUOTES, 'UTF-8') . '</password> </account>  <data type="text">'. htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8') . '</data> </sms>';

It's probably not going to fix the problem itself, but it's likely to present a more sensible error message.

020Error parsing XML document: Error on line 1 of document : Next character must be ">" terminating element "data". Nested exception: Next character must be ">" terminating element "data".
The code doesn't work in one hosting only. May be problem not in a code. Maybe i have to change something in php.init?
Link to comment
Share on other sites

This error message can't be right... line 1 must be the XML prolog... either you haven't altered the code as I showed or something is doing some sort of caching that is showing the error message as it was before the changes. Do

var_dump($postdataIn);

Right after constructing $postdataIn see what it outputs.Also... what exactly parses the XML? Your code or the code you're sending it to? If it's your code, can you ensure that you have PHP5 on all hosts? If your problematic host has PHP4, this might explain the problem.

Link to comment
Share on other sites

This error message can't be right... line 1 must be the XML prolog... either you haven't altered the code as I showed or something is doing some sort of caching that is showing the error message as it was before the changes. Do
var_dump($postdataIn);

Right after constructing $postdataIn see what it outputs.Also... what exactly parses the XML? Your code or the code you're sending it to? If it's your code, can you ensure that you have PHP5 on all hosts? If your problematic host has PHP4, this might explain the problem.

020Error parsing XML document: Error on line 14 of document : Expected "</sms>" to terminate element starting on line 2. Nested exception: Expected "</sms>" to terminate element starting on line 2.
The version of php is 5.2.13There are some more rows, but it doesn't matter.I spent to much time on this stupid problem.Thank you very much to everybody.
Link to comment
Share on other sites

In XML, all rows (i.e. lines) matter. XML is not forgiving for any errors.You still didn't answer my two vital questions... what's the full and exact var_dump() (use dummy data if you have to, but make it one that you're actually giving to your PHP file)? What exactly parses the XML?

Link to comment
Share on other sites

The version of php is 5.2.13There are some more rows, but it doesn't matter.I spent to much time on this stupid problem.Thank you very much to everybody.
No problem is stupid, if you can learn from it, and I was learning from it.So, what are you going to do now, dump one of your hosts?Roddy
Link to comment
Share on other sites

In XML, all rows (i.e. lines) matter. XML is not forgiving for any errors.You still didn't answer my two vital questions... what's the full and exact var_dump() (use dummy data if you have to, but make it one that you're actually giving to your PHP file)? What exactly parses the XML?
OK
The code is$message = ($_POST['msg1']);$text 	= stripcslashes($message);$user 	= "user";$pass 	= "pass";$num	  = "0123456789";$rply	  = "456";$timer	= "0";echo mb_detect_encoding($text);echo "<br />";if ($timer > 0)	$timerStr = "<schedule><relative>" .$timer. "</relative></schedule>";else 	$timerStr = "";$postdataIn = '<?xml version="1.0" encoding="UTF-8"?><sms command="submit" version="1.0"><account><id>'.htmlspecialchars($user, ENT_NOQUOTES, 'UTF-8').'</id><password>'.htmlspecialchars($pass, ENT_NOQUOTES, 'UTF-8').'</password></account>								<attributes><validity type="relative" units="d">1</validity><notify type="0">[0-not_active OR 2-your_email@your_domain OR 4-your_http_address]</notify><reference>123</reference><replyPath>'. htmlspecialchars($rply, ENT_NOQUOTES, 'UTF-8') .'</replyPath></attributes>'.htmlspecialchars($timerStr, ENT_NOQUOTES, 'UTF-8') .'<targets><cellphone>'.htmlspecialchars($num, ENT_NOQUOTES, 'UTF-8').'</cellphone></targets><data type="text">'.htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8').'</data></sms>';$hostIn = "soprano.co.il";$portIn = "80";$pathIn = "/prodsms/corpsms";$postdata = $postdataIn;var_dump($postdataIn);echo "<br />";		doPost($hostIn,$portIn,$pathIn,$postdata);

msg1 comes from form.The response is

UTF-8string(463) " user pass 1 [0-not_active OR 2-your_email@your_domain OR 4-your_http_address] 123456 0123456789 Привет" HTTP/1.0 200 OK Date: Sun, 30 Jan 2011 19:05:57 GMT Server: Jetty/5.1.11RC0 (Linux/2.6.18-4-686 i386 java/1.4.2_16 Content-Type: text/xml;charset=ISO-8859-1 Content-Length: 302 X-Cache: MISS from cached.itip.co.il X-Cache-Lookup: MISS from cached.itip.co.il:1080 Connection: close 020Error parsing XML document: Error on line 14 of document : End of entity not allowed; an end tag is missing. Nested exception: End of entity not allowed; an end tag is missing.
The message "Привет" is right. Why charset=ISO-8859-1 i don't know.
Link to comment
Share on other sites

When I asked for the var_dump() output, I was expecting a copy&paste of the thing you see from "View Source", not a copy&paste of what the browser displays.Manually trying to parse the $postdataIn on my computer shows no errors and I can't notice ones myself either...Depending on the answer to my second vital question - who is parsing the XML (i.e. who is triggering the error that eventually arrives as the output of your script)? - the problem is in one of three places:1. The script that receives this which is incorrectly parsing your valid input to it, with your "problematic" host being the only one that makes PHP do things right.2. Your script... the part that sends this... something somewhere in doPost()... is doing something to the input before sending it... something which fools the receiving server into thinking the input is not UTF-8, and therefore the document is not parsed properly by the remote server.3. Your script... the part that receives the response and parses this or another XML... something somewhere after doPost()... is misreading the server's response.Can you by any chance use cURL instead and save yourself from this and similar issues?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...