Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Object <-> Object comms via PHP

Harris Hare
Second Life Resident
Join date: 5 Nov 2004
Posts: 301
09-07-2006 17:50
Here's a method for sending data between objects by passing it through a webserver. This allows objects anywhere in world to talk to each other relatively instantly without the need for timers or listeners, reducing lag.

You can send a single integer and a single 255 character string on each transmission. The sending uses llHTTPRequest and is subject to throttling. The sending object must know the current XML-RPC channel of the receiving object.

LSL Script:
CODE
key xmlrpcchan;
string url = "http://www.yourwebsite.com/yourphpfile.php";

default {
state_entry()
{
llOpenRemoteDataChannel();
}

remote_data(integer type, key channel, key message_id, string sender, integer ival, string sval)
{
if (type == REMOTE_DATA_CHANNEL) {
xmlrpcchan = channel;
llOwnerSay("Ready. Touch to send.");
} else {
llOwnerSay("Integer Sent: " + (string)ival);
llOwnerSay("String Sent: " + sval);
}
}

touch_start(integer num)
{
//In this example, the sender and reciever are the same script
string chan = (string)xmlrpcchan;
integer int = 123456;
string message = "Hello world!";
llHTTPRequest(url, [HTTP_METHOD, "POST"], "chan=" + chan + "&int=" + (string)int + "&str=" + message);
}

http_response(key id, integer status, list metadata, string body)
{
if (id == NULL_KEY) {
llOwnerSay("HTTPRequest was throttled. Please wait then try again.");
}
}

on_rez(integer param)
{
llResetScript();
}
}


PHP Script to be placed on your server
CODE
<?php
function sendRequest($channel, $intValue, $stringValue) {
$channel = htmlspecialchars($channel);
$int = (int)$intValue;
$string = htmlspecialchars($stringValue);
$data = '<?xml version="1.0"?>';
$data .= '<methodCall>';
$data .= '<methodName>llRemoteData</methodName>';
$data .= '<params><param><value><struct>';
$data .= '<member><name>Channel</name><value><string>'.$channel.'</string></value></member>';
$data .= '<member><name>IntValue</name><value><int>'.$int.'</int></value></member>';
$data .= '<member><name>StringValue</name><value><string>'.$string.'</string></value></member>';
$data .= '</struct></value></param></params></methodCall>';
$fp = fsockopen("xmlrpc.secondlife.com", 80);
fputs($fp, "POST /cgi-bin/xmlrpc.cgi HTTP/1.1\r\n");
fputs($fp, "Host: xmlrpc.secondlife.com\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: ". strlen($data) ."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data);
fclose($fp);
return;
}

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

if ($_POST["chan"] != "") {
sendRequest($_POST["chan"], $_POST["int"], $_POST["str"]);
}
?>
Russell Hansen
Texi pets are here!
Join date: 11 Apr 2006
Posts: 107
09-07-2006 20:38
I've been trying to use something similar between objects, but ensuring the XML-RPC channel is continuously valid is subject to the same issues as keeping keys current as well. If the objects are static, it's OK, but for objects that move or for sporadic communications, you may need to refresh the channel. Not insurmountable problems, but annoying and may end up needing a timer anyway, which takes you back to some of the negatives of other methods such as llEmail.

Still, generally the best way of implementing inter-object communication that I have found yet, in the right circumstances.
_____________________
Russell Hansen - Texi Pet Creator
Texi Pets, your SL Companions and Personal Assistants
http://texipets.com
Redux Dengaku
Registered User
Join date: 28 Sep 2006
Posts: 8
09-28-2006 21:43
I'm having trouble implementing this code... what do I need to do other than changing the yourdomain.com/page.php to the page where the php code is? Do I need to have the cgi-bin/xmlrpc.cgi on my website, or is it pulled off the host website? What type of a response should I expect in SL to know that it was successful?