Difference between revisions of "User:Kuraiko Yoshikawa/LSL/contact from outside"

From Second Life Wiki
Jump to navigation Jump to search
m (→‎LSL Script: Replaced obsolete <lsl> with <syntaxhighlight>)
m (Improve syntax highlighting (removing old, obsolete tags); drastically removed all extraneous <div> and other attempts at non-standard formatting which would mostly be ignored anyway)
 
Line 14: Line 14:
**[[#cURL PHP Script|cURL PHP Script]]
**[[#cURL PHP Script|cURL PHP Script]]
**[[#Limits|Limits]]
**[[#Limits|Limits]]
</div>
</div>


<div id="box">
== Email ==
== Email ==
<div style="padding: 5px;">
If you can send mails via PHP from your webspace/server then sending emails to a script is a simple but not the fastest way.
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.
{{tooltip|Mailaddy|E-mail address}} from a prim is '''uuid@lsl.secondlife.com'''
Mailaddy from a prim is '''uuid@lsl.secondlife.com'''


<div id="box">
== PHP Script ==
== PHP Script ==
<div style="padding: 5px;">
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
<?php
<?php
Line 39: Line 33:
?>
?>
</syntaxhighlight>
</syntaxhighlight>
</div></div>
<div id="box">


== LSL Script ==
== LSL Script ==
<div style="padding: 5px;">
<syntaxhighlight lang="lsl2">
<syntaxhighlight lang="lsl2">
default
default
Line 64: Line 54:
}
}
</syntaxhighlight>
</syntaxhighlight>
</div></div>
<div id="box">


== Limits ==
== Limits ==
<div style="padding: 5px";>
* The email queue is limited to 100 emails, any email after that is bounced.
* 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).
* The message field may have a maximum of 1000 characters. This count includes the header information (address, subject, etc).
</div>
</div>


</div>
== GET Request over http-in ==
</div>
Sending variables/text to an [[LSL]] script via GET request is an easy way to use the 'new' http-in feature.


<div id="box">
However it isn't a way to send lots of data to the script.
== GET Request over http-in ==
<div style="padding: 5px;">
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.


<div id="box">
== PHP Script ==
== PHP Script ==
<div style="padding: 5px;">
<syntaxhighlight lang="php">
<php>
<?php
<?php
$script_url = "http://sim5363.agni.lindenlab.com:12046/cap/<your url cap here>/"; // Don't forget slash at the end
$script_url = "http://sim5363.agni.lindenlab.com:12046/cap/<your url cap here>/"; // Don't forget slash at the end
Line 101: Line 80:


?>
?>
</php>
</syntaxhighlight>
</div></div>


<div id="box">
== LSL Script ==  
== LSL Script ==  
<div style="padding: 5px;">
<syntaxhighlight lang="lsl2">
<lsl>
default
default
{
{
Line 152: Line 128:
     }
     }
}
}
</syntaxhighlight>


</lsl>
</div>
</div>
<div id="box">
== Limits ==
== Limits ==
<div style="padding: 5px";>
* headers (accessed with [[llGetHTTPHeader]]) are limited to 255 bytes. With the script above you can send 255 bytes raw data (include the <nowiki><var></nowiki>=).
* headers (accessed with llGetHTTPHeader) are limited to 255 bytes. With the script above you can send 255bytes raw data (include the <nowiki><var></nowiki>=).
* There is a limit of 64 pending [[http_request]]s
* There is a limit of 64 pending http_request  
</div>
</div>
</div></div>


<div id="box">
== POST Request over http-in ==
== POST Request over http-in ==
<div style="padding: 5px;">
Post request is the best way to send lots of data to an LSL Script however it isn't so a short/simple code to use it within PHP.
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 {{mono|fsockopen()}} or {{mono|cURL}} or connection on another port beyond the default (80 and 443).
Besides not all ISPs allow fsockopen or cURL or connection on other port as the default (80 and 443).
 


<div id="box">
== fsockopen PHP Script ==  
== fsockopen PHP Script ==  
This example is from Simba Fuhr:  [[LSL_http_server/examples#Simple_script_for_sending_data_to_an_LSL_via_PHP|Simple Script for seding data...]]
This example is from Simba Fuhr:  [[LSL_http_server/examples#Simple_script_for_sending_data_to_an_LSL_via_PHP|Simple Script for seding data...]]
<div style="padding: 5px;">
<syntaxhighlight lang="php">
<php>
<?php
<?php
$Data = CallLSLScript("http://sim3015.aditi.lindenlab.com:12046/cap/<your script url cap>", "This is a post request test message via Sockets");
$Data = CallLSLScript("http://sim3015.aditi.lindenlab.com:12046/cap/<your script url cap>", "This is a post request test message via Sockets");
Line 219: Line 183:
}
}
?>
?>
</syntaxhighlight>
</php>
</div>
</div>


<div id="box">
== cURL PHP Script ==  
== cURL PHP Script ==  
<div style="padding: 5px;">
<syntaxhighlight lang="php">
<php>
<?php
<?php
header("Content-Type: text/plain");
header("Content-Type: text/plain");
Line 243: Line 202:
echo $respone;
echo $respone;
?>
?>
</php>
</syntaxhighlight>
</div>
 
</div>
<div id="box">
== Limits ==
== Limits ==
<div style="padding: 5px";>
* body is limited to 2048 bytes; anything longer will be truncated to 2048 bytes. (post request is inside the body)
* 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]]s
* There is a limit of 64 pending http_request  
</div>
</div>
 
</div></div>
 


<div>
[[Category:LSL Examples]]

Latest revision as of 12:39, 30 November 2023

Content:

Email

If you can send mails via PHP from your webspace/server then sending emails to a script is a simple but not the fastest way.

Mailaddy from a prim is uuid@lsl.secondlife.com

PHP Script

<?php

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

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

?>

LSL Script

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);
    }
}

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 an easy way to use the 'new' http-in feature.

However it isn't a way to send lots of data to the script.

PHP Script

<?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;

?>

LSL Script

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");
        }
    }
}

Limits

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

POST Request over http-in

Post request is the best way to send lots of 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 another port beyond the default (80 and 443).

fsockopen PHP Script

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

<?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];
}
?>

cURL PHP Script

<?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;
?>

Limits

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