User:Kuraiko Yoshikawa/LSL/contact from outside

From Second Life Wiki
Jump to: navigation, search

Email

If you can send mails via PHP from your webspace/server then send emails to a script is a simple but not the fastest way. Mailaddy from a prim is uuid@lsl.secondlife.com

PHP Script

<php> <?php

$uuid = "add your prim uuid"; $subject = "Test"; $msg = "Simple Test Mail";

mail($uuid."@lsl.secondlife.com", $subject, $msg);

?> </php>

LSL Script

<lsl> default {

   state_entry()
   {
       llOwnerSay("My UUID is: " + (string)llGetKey());
       llSetTimerEvent(2.0);
   }
   timer() {
       llGetNextEmail("", ""); // Check for emails
   }
   
   email (string time, string address, string subj, string msg, integer num_left)
   {
       llOwnerSay("\nI got an email:\nFrom:" + address + "\nSubject: " + subj + "\nMessage:\n" + msg);
   }

} </lsl>

Limits

  • The email queue is limited to 100 emails, any email after that is bounced.
  • The message field may have a maximum of 1000 characters. This count includes the header information (address, subject, etc).

GET Request over http-in

Sending variables/text to an lsl script via get request is a easy way to use the 'new' http-in feature. However it isn't a way to send many data to the script.

PHP Script

<php> <?php $script_url = "http://sim5363.agni.lindenlab.com:12046/cap/<your url cap here>/"; // Don't forget slash at the end $get_vars = "foo=bar&hello=world";

// Remember, if you intend to send vars with whitespaces or special chars you must encode the string → urlencode($get_vars); // However urlencoded whitespaces/special chars need more space (e.g. comma =  %2C) the active available bytes fall!

// Need allow_url_fopen = true in your php.ini (most free webspace has this deactivated) // If you got an Error ask your ISP about url_fopen $response = file_get_contents($script_url . "?" . $get_vars);

echo $response;

?> </php>

LSL Script

<lsl> default {

   state_entry()
   {
       // Request URL
       llRequestURL();
   }

   http_request(key id, string method, string body)
   {
       if (method == URL_REQUEST_GRANTED)
       {
           llOwnerSay("My URL: " + body);
       }
       else if (method == URL_REQUEST_DENIED)
       {
           llSay(0, "Something went wrong, no url. " + body);
       }
       else if (method == "GET")
       {
           // Get [GET] vars from header
           string get = llGetHTTPHeader(id, "x-query-string");
           
           // Split
           list get_parsed = llParseString2List(get, ["&"], [""]);
           integer i;
           integer pos = llGetListLength(get_parsed);
           
           llOwnerSay("\nReceived GET Request:");            
           for(i = 0; i < pos;i++)
           {
               llOwnerSay(llList2String(get_parsed, i));
           }
           
           //Respone (what you see in your browser)
           llHTTPResponse(id,200,"success");
       }
       else
       {
           llHTTPResponse(id,405,"Unsupported Method");
       }
   }

}

</lsl>

Limits

  • headers (accessed with llGetHTTPHeader) are limited to 255 bytes. With the script above you can send 255bytes raw data (include the <var>=).
  • There is a limit of 64 pending http_request

POST Request over http-in

Post request is the best way to send many data to an LSL Script however it isn't so a short/simple code to use it within PHP. Besides not all ISPs allow fsockopen or cURL or connection on other port as the default (80 and 443).


fsockopen PHP Script

This example is from Simba Fuhr: Simple Script for seding data...

<php> <?php $Data = CallLSLScript("http://sim3015.aditi.lindenlab.com:12046/cap/<your script url cap>", "This is a post request test message via Sockets"); die($Data);

//Function by Simba Fuhr //Use under the GPL License function CallLSLScript($URL, $Data, $Timeout = 10) {

//Parse the URL into Server, Path and Port
$Host = str_ireplace("http://", "", $URL);
$Path = explode("/", $Host, 2);
$Host = $Path[0];
$Path = $Path[1];
$PrtSplit = explode(":", $Host);
$Host = $PrtSplit[0];
$Port = $PrtSplit[1];

//Open Connection
$Socket = fsockopen($Host, $Port, $Dummy1, $Dummy2, $Timeout);
if ($Socket)
{
 //Send Header and Data
 fputs($Socket, "POST /$Path HTTP/1.1\r\n");
 fputs($Socket, "Host: $Host\r\n");
 fputs($Socket, "Content-type: application/x-www-form-urlencoded\r\n");
 fputs($Socket, "User-Agent: Opera/9.01 (Windows NT 5.1; U; en)\r\n");
 fputs($Socket, "Accept-Language: de-DE,de;q=0.9,en;q=0.8\r\n");
 fputs($Socket, "Content-length: ".strlen($Data)."\r\n");
 fputs($Socket, "Connection: close\r\n\r\n");
 fputs($Socket, $Data);

 //Receive Data
 while(!feof($Socket))
  {$res .= fgets($Socket, 128);}
 fclose($Socket);
}

//ParseData and return it
$res = explode("\r\n\r\n", $res);
return $res[1];

} ?>

</php>

cURL PHP Script

<php> <?php header("Content-Type: text/plain");

$msg = "This is a post request test message via cURL";

$curl = curl_init(); curl_setopt($curl, CURLOPT_URL,"http://sim4631.agni.lindenlab.com:12046/cap/<your script url cap>"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $msg);

$respone = curl_exec ($curl); curl_close ($curl); echo $respone; ?> </php>

Limits

  • body is limited to 2048 bytes; anything longer will be truncated to 2048 bytes. (post request are inside the body)
  • There is a limit of 64 pending http_request