LSL Wiki Mirror 10-5-2006: ExamplellHTTPRequest

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings ::
This is a minimalistic approach to setting up a simple request to a remote web server, and get the result.

Limitations: apparently PHP must be compiled as an Apache module (and not using FastCGI) for you to get access to apache_response_headers().
Note: There is an PHP based emulation to the apache_response_headers() while using FastCGI emu_getallheaders()

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

And here goes myscript.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";
?>

Note: if for some reason $_POST[] doesn't return anything, you have to check for other alternatives, like the one described on the first comment on the page for llHTTPRequest (directly reading from the HTTP stream), as per VeloxSeverine's suggestion.

Suggestion: Use $_REQUEST[] instead of $_POST[] as it will work for all types, as well as, POST. SiRiSAsturias

Note: For greater server compatibility and efficiency, use $_SERVER[] instead of apache_request_headers() to access the SL header data. For Example, $_SERVER['HTTP_X_SECONDLIFE_OWNER_NAME']; (notice the format change of the data labels) ~PlexLum

Example: Here is the use of a emulated apache_request_headers() that I wrote; if you're using FastCGI and are used to using the apache function, use this! I hope this helps someone! (It'll also allow you to get the headers if you're not using FastCGI). FoxDiller
<?php
function emu_getallheaders()
{
    foreach(
$_SERVER as $name => $value)
    if(
substr($name05) == 'HTTP_')
        
$headers[str_replace(' ''-'ucwords(strtolower(str_replace('_'' 'substr($name5)))))] = $value;
    return 
$headers;
}

$headers emu_getallheaders(); // Just replace any use of `apache_request_headers()` with `emu_getallheaders()`.

$objectName $headers["X-SecondLife-Object-Name"];
$objectKey $headers["X-SecondLife-Object-Key"];
// ..........................[~ETC ETC~]..........................//
?>

Examples
There are 2 comments on this page. [Display comments/form]