why php works, but from second life no ?
|
|
Dominique Laville
Registered User
Join date: 24 Apr 2008
Posts: 84
|
06-10-2008 08:47
hi guys, I have an object in second life which gets strings from a php server by HTTP request. Everything works fine, but now I need the php script load the strings from a txt file (or mysql database) and then pass them to second life. I just stored them on a txt file and I'm parsing it in order to create an array and pass it to second life. And unluckily it doesn't work. I mean, if I use my browser and i make the request from my browser, everything works. But from second life it doesn't work anymore. (If instead I directly store the data in php variables, it works without any problem). i.e. http://server/myscript.php?action=nextQuestion&next=0answer: This is the first question of the questionnaire. It works perfectly from the browser both if the answer is stored in php variables or in a txt file. In second life instead it works only if they are saved in php variables. Is it maybe a formatting problem ? I don't understand.. I'm frustrated!  ps. If you know an existing system to load questionnaire questions from a web server and upload the answers to it, let me know!
|
|
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
|
06-10-2008 08:56
Hmm. Could MIME type be creating a problem? Have you tried setting the content type to "text/plain"?
|
|
Dominique Laville
Registered User
Join date: 24 Apr 2008
Posts: 84
|
06-10-2008 09:02
I have a mac and I use TextEdit. Which formatting should I use ? Unicode (UTF-16) Unicode (UTF-  Western (Windows Latin1) or others ? how can I set the MIME type ? With my browser everything works great, and I didn't expect a formatting problem since the text is loaded from the php script and printed. In the browser it is just plain text.. What's the problem ?
|
|
Viktoria Dovgal
…
Join date: 29 Jul 2007
Posts: 3,593
|
06-10-2008 09:10
For TextEdit, make sure you Format->Make Plain Text before saving. Otherwise, you'll get RTF and all its weirdness. And, you'll want to give the file an extension (.txt) so that the Web server has a hint that it should serve it as text/plain.
|
|
RobbyRacoon Olmstead
Red warrior is hungry!
Join date: 20 Sep 2006
Posts: 1,821
|
06-10-2008 09:17
Very hard to say without seeing any of your LSL or PHP code, or your php config. For instance, the following PHP code works just fine for me from both the browser and llHttpRequest : $filename = dirname(__FILE__)."/questions.txt";
$fd = fopen ($filename, "r"); $contents = fread ($fd,filesize ($filename)); fclose ($fd);
$delimiter = "\n"; $questions = explode($delimiter, $contents);
$action = $_GET["action"]; $index = (int)$_GET["next"];
if( $action == "nextQuestion" ) { if( $index >= 0 && $index < count($questions) ) { echo $questions[$index]; } }
You really need to give more information for anyone to be able to help. .
|
|
Dominique Laville
Registered User
Join date: 24 Apr 2008
Posts: 84
|
06-10-2008 09:19
Ya, my file is txt and it is plain text. Please help me to find out a solution... i have a lot urgency...
since the questionnaire are quite long maybe it is better if i use a mysql database.. ? I have no experience with that, but if you tell me it is simple to implement it, i will do it.
|
|
Haruki Watanabe
llSLCrash(void);
Join date: 28 Mar 2007
Posts: 434
|
06-10-2008 09:41
uhm - what do you mean by «I just stored them on a txt file and I'm parsing it in order to create an array and pass it to second life». When you call llHTTPRequest() you get the answer from the PHP-Script back in the http_response-event: http_response(key request_id, integer status, list metadata, string body) - here, it is the «body»-variable that you want to observe. in order to put the stuff into the body, you need to output them in PHP... e.g. echo "var1=".$my_var1."&var2=".$my_var2; which would - if called from the browser - look like: var1=the_var1&var2=the_var2 This would then be passed to the SL-script where you can extract the value-pairs using list-functions (for example). Thus being said - you can't pass an array to SL (as your statement makes me think), you have to output the contents of the array in plain-text. When you read a textfile into PHP, you could simply use:
$fp = file("my_file.txt");
$to_sl = implode("&", $fp);
echo $to_sl;
which would read the entire file into an array ($fp), then collapse every line into one single line, the line-contents separated with "&" and then sent back to sl using «echo $to_sl»... Hope I didn't get your post entirely wrong  [Edit] Remember: the amount of data you can send back to SL using llHTTPRequest is limited to 2048 bytes (which isn't that much) - so if your textfile is longer, I sugguest you call llHTTPRequest several times only passing one line at a time back to SL
|
|
Dominique Laville
Registered User
Join date: 24 Apr 2008
Posts: 84
|
06-10-2008 10:03
Hi, yes. it is exactly what I'm doing. I will copy some code to clarify. But I have a question: I used explode instead of implode. Which is the difference ? So, here is the code of the script without loading from a txt file: $questionsArray= array(); $questionsArray[0] = "1. First Question. How are you ?"; $questionsArray[1] = "2. Second question. How old are you ?"; .. //send next question if ($_GET['action']=="nextQuestion"  { echo $questionsArray[$_GET['next']]; } This instead is the code of the txt loading script: $textfile = "questions.txt"; $handle = fopen($textfile,"r"  ; $questions = fread($handle, filesize($textfile)); if (isset($questions)) { $questionsArray = array(); $questionsArray = explode("+", $questions); } ... //send next question if ($_GET['action']=="nextQuestion"  { echo $questionsArray[$_GET['next']]; } So, again. If the $questionsArray[0] string is stored in a local php $variable, everything works great. If it is loaded from a text file in the $varable it doesn't work in Second Life. But it still works in the browser... Since the web service I'm using does not have mySql service I really need to use a txt file..
|
|
Ordinal Malaprop
really very ordinary
Join date: 9 Sep 2005
Posts: 4,607
|
06-10-2008 10:18
Firstly, you can just use $questions = file_get_contents($textfile); to get the whole file as a string, which may be a bit simpler. But otherwise it should work, assuming that your questions are delimited with "+" in the text file properly. Try putting a print_r($questionsArray) after the point at which you read it in, and see what the response is. It shouldn't make a difference how you access it, whether through a browser or through llHTTPRequest. I would also put header("Content-Type: text/plain"  ; at the top by the way, if you haven't.
_____________________
http://ordinalmalaprop.com/forum/ - visit Ordinal's Scripting Colloquium for scripting discussion with actual working BBCode!
http://ordinalmalaprop.com/engine/ - An Engine Fit For My Proceeding, my Aethernet Journal
http://www.flickr.com/groups/slgriefbuild/ - Second Life Griefbuild Digest, pictures of horrible ad griefing and land spam, and the naming of names
|
|
Dominique Laville
Registered User
Join date: 24 Apr 2008
Posts: 84
|
06-10-2008 10:28
1) can I remove the following headers from my php file ? // Only works with PHP compiled as an Apache module $headers = apache_request_headers(); $objectName = $headers["X-SecondLife-Object-Name"]; $objectKey = $headers["X-SecondLife-Object-Key"]; $ownerKey = $headers["X-SecondLife-Owner-Key"]; $ownerName = $headers["X-SecondLife-Owner-Name"]; $region = $headers["X-SecondLife-Region"]; // and so on for getting all the other variables ... 2) my questions.txt file is: + 1. First Question. How are you ? + 2. Second question. How old are you ? + 3. What's your name ? + 4. I think; this is an important, test. + 5. Last question 3) I added header("Content-Type: text/plain"  ; but still nothing.. the text in my browser is changed thought 4) This is the result of print_r($questions); There are asian characters at the end. + First Question. How are you ? + Second question. How old are you ? + What's your name ? + I think; this is an important, test. + Last question䘀椀爀猀琀 儀甀攀猀琀椀漀渀⸀ 䠀漀眀 愀爀攀 礀漀甀 㼀 ⬫㔠⬠潷獲⁴湡睳牥
|
|
Dominique Laville
Registered User
Join date: 24 Apr 2008
Posts: 84
|
06-10-2008 10:31
the crazy thing is that if I overwrite a question:
i.e.
$questionsArray[1] = "This is the first question of the questionnaire. What do you think about it ? ";
The system works perfectly for that question but it doesn't display the others from the text file.
I really think it is a formatting problem... by the way I don't understand. It is just plain text..
|
|
Ordinal Malaprop
really very ordinary
Join date: 9 Sep 2005
Posts: 4,607
|
06-10-2008 10:37
1) If you don't use any of that information in your script you could certainly remove those lines.
4) Not print_r($questions), but print_r($questionsArray) just before you output the question - the idea is to determine whether it is parsing the file correctly into the array, which it doesn't sound like it is really.
_____________________
http://ordinalmalaprop.com/forum/ - visit Ordinal's Scripting Colloquium for scripting discussion with actual working BBCode!
http://ordinalmalaprop.com/engine/ - An Engine Fit For My Proceeding, my Aethernet Journal
http://www.flickr.com/groups/slgriefbuild/ - Second Life Griefbuild Digest, pictures of horrible ad griefing and land spam, and the naming of names
|
|
Dominique Laville
Registered User
Join date: 24 Apr 2008
Posts: 84
|
06-10-2008 10:39
ok, this is crazy!
If i copy/paste the print of the array to the forum, only the first 2 lines are displayed when I post it. So here it is the formatting problem...
Array ( [0] => ÿþ [1] =>
In the browser i see all the lines of the array.
I don't understand the symbols on the first line, but I ignore it by starting from the questionArray[1]
|
|
Dominique Laville
Registered User
Join date: 24 Apr 2008
Posts: 84
|
06-10-2008 10:44
Sorry for the spam. I tried to copy it again, copying/pastying it on another textedit file but it still is incomplete. If I delete the characters before the cut, I have to press delete twice. So they looks like special characters. I don't understand.. it is crazy plain text  Array ( [0] => ÿþ [1] =>F
|
|
Ordinal Malaprop
really very ordinary
Join date: 9 Sep 2005
Posts: 4,607
|
06-10-2008 10:46
Er. Right. Well, that does look like an issue to do with character encoding, which is awkward as I am not very good with that. Try using header("Content-Type: text/plain; charset=utf-8"  ; at the start instead of the header that I suggested earlier.
_____________________
http://ordinalmalaprop.com/forum/ - visit Ordinal's Scripting Colloquium for scripting discussion with actual working BBCode!
http://ordinalmalaprop.com/engine/ - An Engine Fit For My Proceeding, my Aethernet Journal
http://www.flickr.com/groups/slgriefbuild/ - Second Life Griefbuild Digest, pictures of horrible ad griefing and land spam, and the naming of names
|
|
Dominique Laville
Registered User
Join date: 24 Apr 2008
Posts: 84
|
06-10-2008 10:51
I changed the header but still the same problem. There are 2 question marks instead of the symbols on the line 0.
Accidentally I left the print command in the php and I execute the script from second line. I have the same visualization I have here in the forum post (only the first 2 lines are displayed..)
Array ( [0] => �� [1] =>
|
|
Dominique Laville
Registered User
Join date: 24 Apr 2008
Posts: 84
|
06-10-2008 10:56
solved!
I saved the txt file with the correct format UTF-8, and with your correct header and now it works!
thanks very much!
|