LlHTTPRequest
De DigiWiki.
(Différences entre les versions)
(Page créée avec « <lsl> key http_request_id; default { state_entry() { http_request_id = llHTTPRequest("url", [], ""); } http_response(key request_id, integer status,… ») |
|||
Ligne 76 : | Ligne 76 : | ||
?> | ?> | ||
</php> | </php> | ||
+ | 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(). | ||
+ | |||
+ | <lsl> | ||
+ | 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); | ||
+ | } | ||
+ | } | ||
+ | </lsl> | ||
+ | |||
+ | And here goes myscript.php: | ||
+ | |||
+ | <php> | ||
+ | <?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"; | ||
+ | ?> | ||
+ | </php> | ||
+ | |||
+ | 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) | ||
+ | |||
+ | <php> | ||
+ | <?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~]..........................// | ||
+ | ?> | ||
+ | </php> | ||
+ | |||
+ | <php> | ||
+ | <?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"; | ||
+ | } | ||
+ | ?> | ||
+ | </php> | ||
+ | |||
+ | <lsl> | ||
+ | // 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,["\r\n","\n"],[]); | ||
+ | integer count = llGetListLength(lbody); | ||
+ | // This turns newline characters char(10) into new lines in chat | ||
+ | // Fixed Dec 8, 2008: Now parses both Unix and Windows style end of lines | ||
+ | // Though RFC requires Unix style, this becomes more tolerant. | ||
+ | for(i=0;i<count;i++) | ||
+ | { | ||
+ | llOwnerSay(llList2String(lbody,i)); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </lsl> |
Version actuelle en date du 21 juin 2013 à 16:27
key http_request_id; default { state_entry() { http_request_id = llHTTPRequest("url", [], ""); } http_response(key request_id, integer status, list metadata, string body) { if (request_id == http_request_id) { llSetText(body, <0,0,1>, 1); } } }
Example PHP test script:
<?php header("content-type: text/plain; charset=utf-8"); ?> Headers received: <?php /** * @author Wouter Hobble * @copyright 2008 */ foreach ($_SERVER as $k => $v) { if( substr($k, 0, 5) == 'HTTP_') { print "\n". $k. "\t". $v; } } ?>
example wrapper script Both capturing apache headers and global methodes
<?PHP // Author Waster Skronski. // General Public License (GPL). // Mind that some headers are not included because they're either useless or unreliable. $USE_APACHE_HEADERS = TRUE; // switch to false if you need cgi methods if ($USE_APACHE_HEADERS) { $headers = apache_request_headers(); $objectgrid = $headers["X-SecondLife-Shard"]; $objectname = $headers["X-SecondLife-Object-Name"]; $objectkey = $headers["X-SecondLife-Object-Key"]; $objectpos = $headers["X-SecondLife-Local-Position"]; $ownerkey = $headers["X-SecondLife-Owner-Key"]; $ownername = $headers["X-SecondLife-Owner-Name"]; $regiondata = $headers["X-SecondLife-Region"]; $regiontmp = explode ("(",$regiondata); // cut cords off $regionpos = explode (")",$regiontmp[1]); // $regionname = substr($regiontmp[0],0,-1); // cut last space from simname } else { $db = $GLOBALS; $headers = $db['HTTP_ENV_VARS']; $objectgrid = $headers["HTTP_X_SECONDLIFE_SHARD"]; $objectname = $headers["HTTP_X_SECONDLIFE_OBJECT_NAME"]; $objectkey = $headers["HTTP_X_SECONDLIFE_OBJECT_KEY"]; $ownerkey = $headers["HTTP_X_SECONDLIFE_OWNER_KEY"]; $objectpos = $headers["HTTP_X_SECONDLIFE_LOCAL_POSITION"]; $ownername = $headers["HTTP_X_SECONDLIFE_OWNER_NAME"]; $regiondata = $headers["HTTP_X_SECONDLIFE_REGION"]; $regiontmp = explode ("(",$regiondata); $regionpos = explode (")",$regiontmp[1]); $regionname = substr($regiontmp[0],0,-1); } ?>
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().
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:
<?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"; ?>
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)
<?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~]..........................// ?>
<?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,["\r\n","\n"],[]); integer count = llGetListLength(lbody); // This turns newline characters char(10) into new lines in chat // Fixed Dec 8, 2008: Now parses both Unix and Windows style end of lines // Though RFC requires Unix style, this becomes more tolerant. for(i=0;i<count;i++) { llOwnerSay(llList2String(lbody,i)); } } } }