ExamplellHTTPRequest

From lslWiki

Jump to: navigation, search

This example will send information to your php server and reply to second life.

the php script explodes the string with "&x=" this makes it possible for you to transfer multiple pieces of information in the same string and define them as variables on the php server.

Place this script in an object inside Second Life

 
key req_id;
string name;
 
default
{
 
 
    touch_start(integer total_number) {
        name = llDetectedName(0);
        req_id = llHTTPRequest("http://www.yourdomain.com/secondcom.php", [HTTP_METHOD, "POST"],"x=" + (string)llDetectedKey(0)
+ "&y=" + name);
 
    }
    
    http_response(key request_id, integer status, list metadata, string body) {
        if (req_id == request_id) {
            llSay(0, body);   
        }
    }
}
 
 

Save this script as secondcom.php on your server.

 
<?
//Get POST variables and put into superglobal array
$p_data = implode('', file('php://input'));
$p_data = explode('&', $p_data);
 
foreach ($p_data as $p_val) {
    $d_parts = explode('=', $p_val);
    $_POST[$d_parts[0]] = urldecode($d_parts[1]);
}
 
 
 
$key = $_POST['x'];
$name = $_POST['y'];
 
$return = "Key: ".$key." | Name: ".$name." have touched me";
 
echo $return;
?>
 
 

--Mr.Bichel 17:46, 4 February 2007 (EST)


これはリモートなウェブサーバに単純なリクエストを準備するシンプルな手法です。

 
key requestid; // just to check if we're getting the result we've asked for; all scripts in the same object get the same replies
 
default
{
    touch_start(integer number)
    {
        requestid = llHTTPRequest("http://my.server.com/my-script.php", 
            [HTTP_METHOD, "POST",
             HTTP_MIMETYPE, "application/x-www-form-urlencoded"],
            "parameter1=hello&parameter2=world");
    }
 
    http_response(key request_id, integer status, list metadata, string body)
    {
        if (request_id == requestid)
            llWhisper(0, "Web server said: " + body);
    }
}
 
 

以下はサーバサイドのスクリプト(PHP)のサンプルです

 
<?
// Only works with PHP compiled as an Apache module
$headers = apache_request_headers();
 
$objectName = $headers["X-SecondLife-Object-Name"];
$objectKey     = $headers["X-SecondLife-Object-Key"];
$ownerKey     = $headers["X-SecondLife-Owner-Key"];
$ownerName = $headers["X-SecondLife-Owner-Name"];
$region        = $headers["X-SecondLife-Region"];
// and so on for getting all the other variables ...
 
// get things from $_POST[]
// Naturally enough, if this is empty, you won't get anything
$parameter1    = $_POST["parameter1"];
$parameter2    = $_POST["parameter2"];
 
echo $ownerName . " just said " . $parameter1 . " " . $parameter2 . "\n";
?> 
 
 
Personal tools