Go Back   SL Forums > Resident Forums > Content Creation > Scripting Tips
Welcome, Al Supercharge.
You last visited: 05-07-2008 at 06:33 AM
User CP GUIDELINES - please read before posting New Posts Quick Links Log Out


Reply
Thread Tools Search this Thread Display Modes
Unread 06-10-2008, 03:47 PM   #1
Dominique Laville
Registered User

Join Date: Apr 2008
Posts: 69
Angry why php works, but from second life no ?

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.
[url]http://server/myscript.php?action=nextQuestion&next=0[/url]

answer: 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!
Dominique Laville is offline Report Bad Post   Reply With Quote
Unread 06-10-2008, 03:56 PM   #2
Hewee Zetkin
Registered User

Join Date: Jul 2006
Posts: 1,327
Hmm. Could MIME type be creating a problem? Have you tried setting the content type to "text/plain"?
Hewee Zetkin is offline Report Bad Post   Reply With Quote
Unread 06-10-2008, 04:02 PM   #3
Dominique Laville
Registered User

Join Date: Apr 2008
Posts: 69
I have a mac and I use TextEdit. Which formatting should I use ?

Unicode (UTF-16)
Unicode (UTF-8)
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 ?
Dominique Laville is offline Report Bad Post   Reply With Quote
Unread 06-10-2008, 04:10 PM   #4
Viktoria Dovgal
[RESOLVED]

Join Date: Jul 2007
Location: Your desired location is unavailable. You have been moved to a nearby region.
Posts: 1,076
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.
Viktoria Dovgal is offline Report Bad Post   Reply With Quote
Unread 06-10-2008, 04:19 PM   #5
Dominique Laville
Registered User

Join Date: Apr 2008
Posts: 69
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.
Dominique Laville is offline Report Bad Post   Reply With Quote
Unread 06-10-2008, 04:41 PM   #6
Haruki Watanabe
llSLCrash(void);

Join Date: Mar 2007
Posts: 315
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:

[php]

$fp = file("my_file.txt");

$to_sl = implode("&", $fp);

echo $to_sl;

[/php]

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
Haruki Watanabe is offline Report Bad Post   Reply With Quote
Unread 06-10-2008, 05:03 PM   #7
Dominique Laville
Registered User

Join Date: Apr 2008
Posts: 69
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..
Dominique Laville is offline Report Bad Post   Reply With Quote
Unread 06-10-2008, 05:18 PM   #8
Ordinal Malaprop
really very ordinary

Join Date: Sep 2005
Location: Caledon
Posts: 4,230
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.
Ordinal Malaprop is online now Report Bad Post   Reply With Quote
Unread 06-10-2008, 05:28 PM   #9
Dominique Laville
Registered User

Join Date: Apr 2008
Posts: 69
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 is offline Report Bad Post   Reply With Quote
Unread 06-10-2008, 05:31 PM   #10
Dominique Laville
Registered User

Join Date: Apr 2008
Posts: 69
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..
Dominique Laville is offline Report Bad Post   Reply With Quote
Unread 06-10-2008, 05:37 PM   #11
Ordinal Malaprop
really very ordinary

Join Date: Sep 2005
Location: Caledon
Posts: 4,230
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.
Ordinal Malaprop is online now Report Bad Post   Reply With Quote
Unread 06-10-2008, 05:39 PM   #12
Dominique Laville
Registered User

Join Date: Apr 2008
Posts: 69
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]

Last edited by Dominique Laville : 06-10-2008 at 05:44 PM.
Dominique Laville is offline Report Bad Post   Reply With Quote
Unread 06-10-2008, 05:44 PM   #13
Dominique Laville
Registered User

Join Date: Apr 2008
Posts: 69
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

Last edited by Dominique Laville : 06-10-2008 at 05:47 PM.
Dominique Laville is offline Report Bad Post   Reply With Quote
Unread 06-10-2008, 05:46 PM   #14
Ordinal Malaprop
really very ordinary

Join Date: Sep 2005
Location: Caledon
Posts: 4,230
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.
Ordinal Malaprop is online now Report Bad Post   Reply With Quote
Unread 06-10-2008, 05:51 PM   #15
Dominique Laville
Registered User

Join Date: Apr 2008
Posts: 69
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 is offline Report Bad Post   Reply With Quote
Reply



Posting Rules
You may post new threads
You may post replies
You may post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump



All times are GMT. The time now is 09:56 PM.


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright 2002-2007 Linden Lab