Reading headers in a PHP script
When an http request is made from a script within a secondlife to an external web server, the simulator adds some headers such as X-Secondlife-Shard or X-Secondlife-Owner-Key. In this way I save the work of having to send that data in the POST.
There are many examples doing a few searches. One of the most popular codes uses a huge "if" to distinguish when the variables arrive in Apache form or arrive entirely in uppercase with the prefix "HTTP_". I distrust that solution for being Spaghetti code.
Another more elegant solution reimplements the apache_request_headers () function by checking before it does not exist. Only one problem, in my environment it does not work. This function is implemented but the headers from secondlife are in uppercase format.
Finally I had to create my own version. I include the code of two PHP scripts and an lsl script.
Finaly, login in secondlife and create a prim with the next script inside.
There are many examples doing a few searches. One of the most popular codes uses a huge "if" to distinguish when the variables arrive in Apache form or arrive entirely in uppercase with the prefix "HTTP_". I distrust that solution for being Spaghetti code.
Another more elegant solution reimplements the apache_request_headers () function by checking before it does not exist. Only one problem, in my environment it does not work. This function is implemented but the headers from secondlife are in uppercase format.
Finally I had to create my own version. I include the code of two PHP scripts and an lsl script.
File: slHeaders.php
The following file is included as is on the web server
<?PHP class SlData { public $ObjectGrid; public $ObjectName; public $ObjectKey; public $ObjectPos; public $OwnerKey; public $OwnerName; public $RegionData; public $RegionName; function __construct() { $headers = $this->getSecondLifeHeaders(); $this->ObjectGrid = $headers["X-Secondlife-Shard"]; $this->ObjectName = $headers["X-Secondlife-Object-Name"]; $this->ObjectKey = $headers["X-Secondlife-Object-Key"]; $this->OwnerKey = $headers["X-Secondlife-Owner-Key"]; $this->OwnerName = $headers["X-Secondlife-Owner-Name"]; $this->RegionData = $headers["X-Secondlife-Region"]; $regiontmp = explode("(", $this->RegionData); $this->RegionName = substr($regiontmp[0], 0, -1); $this->ObjectPos = $headers["X-Secondlife-Local-Position"]; $this->ObjectPos = trim($this->ObjectPos, "()"); $xyz = explode(", ", $this->ObjectPos); $this->ObjectPos = sprintf('<%0.2F, %0.2F, %0.2F>', $xyz[0], $xyz[1], $xyz[2]); } private function getSecondLifeHeaders() { foreach($_SERVER as $key=>$value) { if (substr($key,0,17) == "HTTP_X_SECONDLIFE") { $key = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($key,5))))); $out[$key] = $value; } else if (substr(strtolower($key), 0, 12) == "x-secondlife") { $Key = ucwords(strtolower($key)); $out[$key] = $value; } } return $out; } } ?>
File: headertest.php
This other file is an example of use and serves to check the operation of the previous script.<?PHP //ini_set('display_errors',1); //error_reporting(E_ALL); include 'slHeaders.php'; try { $sl_data = new SlData(); echo "\n===== RESULT =====\n"; echo "GRID: " . "$sl_data->ObjectGrid" . "\n"; echo "OBJECT: " . "$sl_data->ObjectName" . "\n"; echo "OBJKEY: " . "$sl_data->ObjectKey" . "\n"; echo "POSITION: " . "$sl_data->ObjectPos" . "\n"; echo "OWNER KEY: " . "$sl_data->OwnerKey" . "\n"; echo "OWNER NAME: " . "$sl_data->OwnerName" . "\n"; echo "REGION: " . "$sl_data->RegionName" . "\n"; echo "==================\n"; } catch(Exception $e) { echo "Se ha producido una excepción. \n"; //var_dump($e); } ?>
Finaly, login in secondlife and create a prim with the next script inside.
// Change this. string URL = "https://changethis/headertest.php"; key http_request_id; key doSimpleGet(string url) { string body = "HELLO WORLD"; key request_id = llHTTPRequest(url, [ HTTP_METHOD, "POST", HTTP_PRAGMA_NO_CACHE, TRUE, HTTP_MIMETYPE, "application/x-www-form-urlencoded" ], body); return request_id; } // doPost default { state_entry() { llSetText("Click me", <1.0, 1.0, 0.0>, 1.0); llTargetOmega(<0,0,1>, PI/8, 1.0); } // state_entry touch_end(integer numDetected) { string avatarGuid = llDetectedKey(0); if (avatarGuid != llGetOwner()) { return; } // if http_request_id = doSimpleGet(URL); } // touch_start http_response(key request_id, integer status, list metadata, string body) { if (request_id != http_request_id) return; if (status != 200) { llOwnerSay("Status: " + (string) status + " Body: " + body); return; } // if llOwnerSay(body); } // http_response } // default
I have used Visual Studio Code to add code with syntax highlighting. But it hasn't worked too well by adding extra lines. I am looking for an alternative.
ReplyDeleteI found two alternatives.
Delete1 - tohtml.com I use this to solve the problem in this post.
2 - http://pygments.org This is a downloadable command line application based on python. This support many languajes and can add your favorite languaje if not on the list of supported languajes.