Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Using llHTTPRequest to store and Retreive Data in SL

Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
11-06-2007 19:43
Ok so how do I go about taking the data from the response and put that into a strings or variables in LSL. for example using the script below:

string ownerkey; // Ownerkey Global Variable

key requestid;

default
{

state_entry()
{
ownerkey = llKey2Name(llGetOwner()); //Get the Owner of the Objects Username
}
touch_start(integer number)
{
requestid = llHTTPRequest("http://BLAHBLAHBLAH.com/test.php",
[HTTP_METHOD, "POST",
HTTP_MIMETYPE, "application/x-www-form-urlencoded"],
"username="+ownerkey+"&apples=18";);
}

http_response(key request_id, integer status, list metadata, string body)
{
if (request_id == requestid)
llWhisper(0, "Web server said: " + body);
}
}


In this section how Do I take whats returned as the body variable into seperate variables for Username and apples? So I can call on how many applpes came back from the php file?

Thank you
-Cherry Hotaling
nand Nerd
Flexi Fanatic
Join date: 4 Oct 2005
Posts: 427
11-07-2007 02:35
Now you have the fun task of learning string manipulation. You'll need to chop up the string returned by the http_response and syphon off the values to the corresponding variables.

You have a few options on how to go about doing so:

Firstly you could use llGetSubString to extract a certain number of characters from a string, this is particularly useful if you know the format of the string. llGetSubString has a useful counterpart, llSubStringIndex, this will search for the first occurrence of a character or group of characters (string) within a string and return the index. This could be used for finding markers which you place in your return string (such as '=' or ',') by which you can split your string up into valuename=value pairs. However, this leads us to the next option...

Second option is to use llParseString2List which will chop up a string using separators which you specify (again this could be '=' and ',' or anything which wouldn't normally be passed as a value). With your string turned into a list you should be able to pick out your corresponding values using llList2String. If you use a predefined format for the string (i.e. you put the userkey first followed by a = then the integer for number of apples) you can simply index directly. If you wish to expand on this you can use a for loop to run through the valuenames and pluck out the values (since they would be organised in the list as [valuename, value, valuename2, value2...].

There could of course be other options. Personally I'd go for the second option with the valuename=value pair and for loop however this requires an understanding of lists.

edit: forgot to add the links:
http://wiki.secondlife.com/wiki/LlGetSubString
http://wiki.secondlife.com/wiki/LlSubStringIndex
http://wiki.secondlife.com/wiki/LlParseString2List
http://wiki.secondlife.com/wiki/Category:LSL_String
_____________________
www.nandnerd.info
http://ordinalmalaprop.com/forum - Ordinal Malaprop's Scripting Forum
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
11-07-2007 08:05
ahhh ok nand so in the php script I could have it echo the values for username and amount of apples like this:

echo "$username,$appleamount";

Output:
Cherry hotaling,6

(Havent tested the php part using the = if that would work in an echo or say its equal to appleamount.)

But it would be something like that?

Then I would use llParseString2List to seperate the , and put it together in a list?
Can llParseString2List store numbers because its coming back like a string?

Thanks,
-Cherry Hotaling
nand Nerd
Flexi Fanatic
Join date: 4 Oct 2005
Posts: 427
11-07-2007 15:49
From: Cherry Hotaling
...

Yep you're getting the jist off it.

From: someone

Can llParseString2List store numbers because its coming back like a string?

This is where you need to be able to tell the script how to expect the list. If you're telling it "name , number of apples" then you parse it into a list you'll end up with a list of strings which you can then use llList2String into variables, which you do as such:
CODE

string name = llList2String(listname,0);
integer numberofapples = (integer)llList2String(listname,1);

The reason for typecasting (using (integer) ) instead of llList2Integer I can't quite recall but the important part here is that you are hard-coding the order in which you receive values. If you accidentally changed the PHP to send the number of apples before the name then this script would be broken.

With a valuename=value pair you do the following:
CODE

list namevaluepairs = llParseString2List(data, [",", "="], []);
integer listlength = llGetListLength(namevaluepairs);
integer i;
string name;
integer numberofapples;
for(i=0;i<listlength;i++)
{
if(llList2String(namevaluepairs,i) == "name")
{
i++;
name = llList2String(namevaluepairs,i);
}
else if (llList2String(namevaluepairs, i) == "apples")
{
i++;
numberofapples = (integer)llList2String(namevaluepairs, i);
}
}
_____________________
www.nandnerd.info
http://ordinalmalaprop.com/forum - Ordinal Malaprop's Scripting Forum
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
11-08-2007 07:17
well The values coming back from the mysql server are going to be all numbers for stats. Like Health, Mana, Swordsmanship, Tactics etc.

What I want to do is create a HUD that polls my php script and asks if the user exists in the system. If they dont exist then create them in the table with all thier stats set to 0.

If they do exist poll the Mysql server for thier stats. The stats will all be numbers such as:
100,100,53,42,0,0,0,0
These will represent the numbers for thier stats that in SL will be inserted into variables that correspond for each stat like:
Health, Mana, Archery, Tactics, swordsmanship, woodworking, magery, spellresist

then the above variables like health mana etc will be global variables for all the scripts in my HUD.

So should I use: llList2Integer or llList2Float instead? (Since some of them might even be float values)

-Cherry Hotaling
nand Nerd
Flexi Fanatic
Join date: 4 Oct 2005
Posts: 427
11-08-2007 15:19
From: Cherry Hotaling
So should I use: llList2Integer or llList2Float instead? (Since some of them might even be float values)


I would use (integer)llList2String(...) and (float)llList2String(...). Mainly because the list contains strings (although the contents of the string is an integer or a float).

llParseString2List gives ["0.2", "1", "4", "0"...] not [0.2, 1, 4, 0...]
_____________________
www.nandnerd.info
http://ordinalmalaprop.com/forum - Ordinal Malaprop's Scripting Forum
FlipperPA Peregrine
Magically Delicious!
Join date: 14 Nov 2003
Posts: 3,703
11-08-2007 15:33
From: Cherry Hotaling
Hi FlipperPA Peregrine,

I appreciate your AD for your cause. However what does your reply have to do with my question in any way?

-Cherry Hotaling

We have the ability to start outgoing HTTP requests, and incoming are a logical companion, yes? It would make getting data from a database using PHP into an object in world much more efficient, yes?

I'm guessing a lot of people would be interested. I didn't know about this "cause" until it was pointed out to me in another thread, so I figured I'd pass a good idea along to parties that would be interested. Sorry if you didn't consider it germane to the discussion. I'll try to pay mind to the forum hall monitor more in the future. :)

Regards,

-Flip
_____________________
Peregrine Salon: www.PeregrineSalon.com - my consulting company
Second Blogger: www.SecondBlogger.com - free, fully integrated Second Life blogging for all avatars!
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
11-09-2007 06:35
The only other question I can think to ask is what would it look like (The prim script)(I havent used the llList2String yet in a script) taking data back from my php script:

100,100,53,42,0,0,0,0
and turning it into variable like this:
Health, Mana, Archery, Tactics, swordsmanship, woodworking, magery, spellresist

Thank you in advance.
-Cherry
nand Nerd
Flexi Fanatic
Join date: 4 Oct 2005
Posts: 427
11-09-2007 07:04
From: Cherry Hotaling
The only other question I can think to ask is what would it look like (The prim script)(I havent used the llList2String yet in a script) taking data back from my php script:

100,100,53,42,0,0,0,0
and turning it into variable like this:
Health, Mana, Archery, Tactics, swordsmanship, woodworking, magery, spellresist

Thank you in advance.
-Cherry


CODE


http_response(...string data...)
{
list datalist = llParseString2List( data, [","], [] );
Health = (integer)llList2String(datalist,0);
Mana = (integer)llList2String(datalist,1);
...
}



Take a look at the wiki entries for llParseString2List and the http_response for the full details of how to implement the function/event, the above is just how to approach the problem.
_____________________
www.nandnerd.info
http://ordinalmalaprop.com/forum - Ordinal Malaprop's Scripting Forum
Cherry Hotaling
Registered User
Join date: 25 Feb 2007
Posts: 86
11-09-2007 12:50
Hey there nand

So the following:
Health = (integer)llList2String(datalist,0);
Mana = (integer)llList2String(datalist,1);

the 0 represents the first number on the left from the response of the php script?
the 1 represents the second number right?

I will take a look to understand the command better.

Thank you so much hun

-Cherry Hotaling
FlipperPA Peregrine
Magically Delicious!
Join date: 14 Nov 2003
Posts: 3,703
11-09-2007 13:26
From: Cherry Hotaling
So the following:
Health = (integer)llList2String(datalist,0);
Mana = (integer)llList2String(datalist,1);

the 0 represents the first number on the left from the response of the php script?
the 1 represents the second number right?

That is correct, with 2 being the third index, through (length of list - 1).

One other tip: you may want to consider using a less standard delimiter than a comma. Many in SL use the "pipe" (what you get when you press shift + \) character: |. Or a few together. The "|" character is much less likely to appear in any kind of text field than a comma, especially if you use a few of them as your delimiter.

Regards,

-Flip
_____________________
Peregrine Salon: www.PeregrineSalon.com - my consulting company
Second Blogger: www.SecondBlogger.com - free, fully integrated Second Life blogging for all avatars!
1 2