Jump to content

LSL to PHP back to LSL


Gayngel
 Share

You are about to reply to a thread that has been inactive for 559 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Recently I've been learning PHP and SQL and I now have a project that requires an external server as LSD wont have sufficient data storage. I can create and retrieve data from a table within my PHP script but I have no clue how to connect it to an inworld source.

I've looked at some of the PHP server examples on the wiki but they are just too involved for me to understand.

I would like a simple example of POSTING/GETTING a message to a PHP script (for example llHTTPRequest(example.com/example.php, [HTTP_METHOD,"POST"], "Greet" )) and an example  php script that receives the post and sends a message back ("Hello World") to the LSL script and then I reckon I can build up to retrieving data from the database from there.

Can anyone show me how to do this? Thanks in advance.

Edit: I know the LSL parts, keeping track of the URL and all that, I'm really more interested in the PHP side.

Edited by Gayngel
  • Like 1
Link to comment
Share on other sites

@Gayngel

dunno if this helps, but here is a very simple example... ( the url i'm using is a quickie server i made on replit )

the send/recieve is all json

LSL:

key    http_request_id;
string URL = "https://SecLife-php-1.tikihed.repl.co/index.php";

default
{
    state_entry()
    {     
    }
    touch_start(integer total_number)
    {  llOwnerSay("Sending...");
       string j_obj = llList2Json( JSON_OBJECT,     
           [  "name",   llDetectedName(0),
              "region", llGetRegionName()     
           ]);
       http_request_id = llHTTPRequest(URL ,
       [ HTTP_METHOD,          "POST", 
         HTTP_MIMETYPE,        "application/x-www-form-urlencoded", 
         HTTP_BODY_MAXLENGTH,  16384,
         HTTP_PRAGMA_NO_CACHE, TRUE
       ], j_obj ); 
    }
    http_request(key id, string method, string body)
    { 
        integer responseStatus = 200;
        string responseBody    = "my method"; 
       
        if (method == "GET")
        {  responseStatus = 200;
           responseBody   = "ok";
        }
        // else if (method == "POST") ...;
        // else if (method == "PUT") ...;
        // else if (method == "DELETE") { responseStatus = 403; responseBody = "forbidden"; } 
        llHTTPResponse(id, responseStatus, responseBody);
    }
    http_response(key request_id, integer status, list metadata, string body) 
    {  llOwnerSay("\nHTTP Resp: \n \n" + body);   
    }
}

PHP:

<?php
  if ($_SERVER["REQUEST_METHOD"] == "POST") {    
     $json = file_get_contents('php://input');
     $data = json_decode($json);
       foreach($data as $key=>$value){
          echo $key . " : " . $value . "\n";
        }
  }	 
?>

 

Edited by Xiija
  • Like 1
  • Thanks 3
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 559 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...