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¶meter2=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.
SiRiSAsturiasNote:
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) ~
PlexLumExample: 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).
FoxDillerSmall edit to correct the casing of X-SecondLife headers, as
variable names and array keys in PHP are case sensitive. (And in case anyone is
wondering, this function also works with PHPSuExec too) - EddyOfarrel
<?php
function emu_getallheaders()
{
foreach($_SERVER as $name => $value)
if(substr($name, 0, 5) == 'HTTP_')
$headers[str_replace('X-Secondlife-', 'X-SecondLife-', str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))))] = $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~]..........................//
?>
I have been
asked to offer my source code for checking HTTP headers, so I present yet
another example... the server and client code to check headers... - Ice
<?php
//This simply echoes the headers exactly as it gets them...
//This script requires PHP as an apache module to work
$headers=apache_request_headers();
foreach($headers as $key => $val)
{
echo $key.": ".$val."\n";
}
?>
//Ice Brodie's http text output script
//This script requests from our data source (URL) and echoes out what it sends to the owner of the script
//I use this to test my PHP apps personally, it's a simple debugging application so I release it publicly
//You may use, redistribute, modify, copy however you feel would be useful
string URL="http://www.secondlife.com/httprequest/homepage.php";
//Replace URL with the path to your web server.
key http;//This stores the HTTP request we send.
default
{
touch_start(integer foo)
{
http=llHTTPRequest(URL, [] ,"");
}
http_response(key id,integer status, list meta, string body)
{
if(http==id)
{
integer i;
list lbody=llParseString2List(body,["\n"],[]);
integer count=llGetListLength(lbody);
//This turns newline characters char(10) into new lines in chat
//These are Unix/LSL style end of lines, use \r\n in place of \n for
// Windows style end of line from web server.
for(i=0;i<count;i++)
{
llOwnerSay(llList2String(lbody,i));
}
}
}
}
--
Examples