User:Opensource Obscure/http/text-db

From Second Life Wiki
Jump to navigation Jump to search

Go back to Opensource Obscure's userpage



Web Form to send messages in-world from a webpage

The following LSL script requests a new LSL Server public URL, then makes an HTTP call that updates a DB with the public URL of the script. A form read the URL from the DB and lets users send a message from a web page to the script.
It supports an automatic system to work around the dynamic URL problem. This requires a web hosting with PHP/MySQL support and a field in a database.
It sort-of works (only in a few sims on the beta grid!), but it's not complete yet; also, it's probably horrible code, I'm not a developer - don't use this if you don't know what you're doing.


LSL code

<lsl>

   string my_url;
   key http_request_id;
   string base_url = "http://..../update.php?q="; // LA TUA PAGINA PHP
   default
   {
       state_entry()
       {
           llRequestURL();     
       }
       http_request(key id, string method, string body)
       {
           if (method == URL_REQUEST_GRANTED)
           {
               my_url = body;
               llOwnerSay("chiamero' questo indirizzo: " + base_url + my_url);
               llSetObjectDesc(my_url);
               http_request_id = llHTTPRequest(base_url + my_url, [HTTP_METHOD, "POST"], "");             
           }
           else if (method == URL_REQUEST_DENIED)
           {
               llOwnerSay("Errore: non ricevo il mio URL. " + body);
           }
           else if (method == "GET")
           {
               string text = llGetHTTPHeader(id, "x-query-string");
               llSetText(text, <1,1,1>, 1);
               llHTTPResponse(id,200,"OK!");
               llSay(0, "sono stato chiamato da web");
           }
           else
           {
               llHTTPResponse(id,405,"Unsupported Method");
           }
       }
       touch_start(integer total_number)
       {
           llOwnerSay("Il mio URL e': " + my_url);
           http_request_id = llHTTPRequest(base_url + my_url, [], "");           
       }


       http_response(key request_id, integer status, list metadata, string body)
       {
           if (request_id == http_request_id)
           {
               // llInstantMessage(llGetOwner(), body);
           }
       }       


   }

</lsl>


HTML / PHP code

<lsl>

   <html>
   <head>
   <title></title>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   </head>
   <body>
   <?php
   include 'config.php';
   include 'opendb.php';
   $query  = "SELECT * FROM `http-in`";
   $result = mysql_query($query);
   while($row = mysql_fetch_array($result, MYSQL_ASSOC))
   {
       echo "<a href=\"{$row['url']}\">{$row['url']}</a>

"; $newurl = $row['url']."/test"; }
   include 'closedb.php';
   ?>


   <form action ="<?php echo $newurl ?>" method="get">
   <input name="text">
   <input type="submit">
   </form>
   </body>
   </html> 

</lsl>


Headers (http request)

LSL

<lsl>string baseurl = "http://....file.php?"; key http_request_id; string dato = "HTTP_X_SECONDLIFE_OWNER_KEY";

default {

   state_entry()
   {
       llSay(0, "Hello, Avatar!");
   }
    touch_start(integer total_number)
       {
           http_request_id = llHTTPRequest(baseurl + dato, [], "");           
       }


       http_response(key request_id, integer status, list metadata, string body)
       {
           if (request_id == http_request_id)
           {
                llInstantMessage(llGetOwner(), body);
           }
       }       

} </lsl>

HTML

<lsl><HTML><HEAD></HEAD><BODY>

<?php

foreach ($_SERVER as $k => $v) { if( $k == $_SERVER['QUERY_STRING']) { print $k. "\n"; print $v; } }

?>

</BODY> </HTML> </lsl>