I will explain the code later. Here's the code so you can start playing with it. I'm at work right now, so I can't check the code, but I will mess with it later tonight and finish this page up. This is basically LSL and PHP code that can be used to communicate between an object in
SecondLife and your web server.
Jakarta Hotel |
Rumah Dijual |
Properti Semarang |
Parfum |
Perlengkapan Bayi |
GPS Tracking |
Outsourcing service Indonesia
LSL code for your in-game object.
// This is the email address that we send our initial notification request to. Your PHP script will read and parse the email at
// this address later through POP3, so you should probably use one that is not used by anyone or anything else.
string NOTIFY_EMAIL = 'whatever@yourdomain.com';
// I always set constants for different idata commands sent through RPC. Then I just make sure they match the constants
// that I set in my PHP file, and everything works great.
integer RPC_ERROR = 0;
integer RPC_COMMAND1 = 1;
integer RPC_COMMAND2 = 2;
// This will be used as a reference later in case we need to know it.
key rpc_chan;
default {
state_entry()
{
// First, open an RPC channel so the website can contact us.
llOpenRemoteDataChannel();
}
remote_data(integer event_type, key channel, key message_id, string sender, integer idata, string sdata)
{
// If the RPC channel has just been opened, then we need to send off a notice to the webserver.
if (event_type == REMOTE_DATA_CHANNEL)
{
// We should keep a reference to the RPC channel that has been opened.
rpc_chan = channel;
// Here, we can specify extra parameters in the email that we need the webserver to know, such as the RPC channel.
string msg;
msg = "Command: OpenRPCChannel\n";
msg += "RPC-Chan: "+(string)rpc_chan+"\n";
llEmail(NOTIFY_EMAIL, "RPC Chan Opened", msg);
llSay(0, "Channel has been opened, and email has been sent. Waiting for ET to phone home.");
}
// If an RPC request has been sent from the webserver.
else if (event_type == REMOTE_DATA_REQUEST)
{
// We can check what command is coming through here, and handle them as needed.
if (idata == RPC_COMMAND1)
{
llSay(0, "We have received a request for RPC_COMMAND1. Sending acknowledment.");
llRemoteDataReply(channel, message_id, "success", RPC_COMMAND1);
// Just checking for another command.
}
else if (idata == RPC_COMMAND2)
{
llSay(0, "We have received a request for RPC_COMMAND2. Sending acknowledment.");
llRemoteDataReply(channel, message_id, "success", RPC_COMMAND2);
// If it was an unrecognized command, we still have to send some sort of reply back.
}
else
{
llSay(0, "An unrecognized RPC command was sent. Sending back error reply.");
llRemoteDataReply(channel, message_id, "", RPC_ERROR);
}
}
}
}
PHP code for your web server. Run this from the command prompt, not from the browser!
<?php
// These are the same as the RPC commands we defined in the LSL script.
define('RPC_ERROR', 0);
define('RPC_COMMAND1', 1);
define('RPC_COMMAND2', 2);
// This is the hostname, username and password to login to your POP3 email account.
define('POP3_HOST', 'localhost');
define('POP3_PORT', 110);
define('POP3_USER', 'whatever@yourdomain.com');
define('POP3_PASS', 'password');
// This is the SecondLife RPC url.
define('SL_URL', 'http://xmlrpc.secondlife.com/cgi-bin/xmlrpc.cgi');
// Minimum amount of time for each loop through the 'while' below.
define('MIN_LOOP_TIME', 10);
// Just a generic command to send an RPC request to SL and retrieve a reply response.
// Requires CURL.
function sendrpc($chan, $intval, $strval) {
$req = xmlrpc_encode_request("llRemoteData", array("Channel"=>(string)$chan, "IntValue"=>(int)$intval, "StringValue"=>(string)$strval));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, SL_URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$reply = curl_exec($ch);
curl_close($ch);
$replyVars = xmlrpc_decode($reply);
return $replyVars;
}
// We want to continuously loop, checking for emails.
while (true) {
echo "Checking mail...\n";
// Get the starting time.
$starttime = time();
// Open your POP3 email box.
$mbox = imap_open("{".POP3_HOST."/pop3:".POP3_PORT."/notls}INBOX", POP3_USER, POP3_PASS);
$msg_headers = imap_headers($mbox);
for ($i=1; $i <= count($msg_headers); $i++) {
$temp = imap_fetchheader($mbox, $i);
// Match email headers for emails coming from SL.
if (preg_match("/From: .*? <(.*?)@lsl.secondlife.com>/i", $temp, $regs) > 0) {
// Extract information that can be used later, like for storing in a DB.
$info = array();
$info['object_key'] = $regs[1];
$temp = imap_body($mbox, 1);
if (preg_match("/Object-Name: (.*?)\r/i", $temp, $regs) > 0) {
$info['object_name'] = $regs[1];
}
if (preg_match("/Region: (.*?) \((.*?), (.*?)\)\r/i", $temp, $regs) > 0) {
$info['region_name'] = $regs[1];
$info['region_x'] = $regs[2];
$info['region_y'] = $regs[3];
}
if (preg_match("/Local-Position: \((.*?), (.*?), (.*?)\)\r/i", $temp, $regs) > 0) {
$info['local_x'] = $regs[1];
$info['local_y'] = $regs[2];
$info['local_z'] = $regs[3];
}
// Check for the command parameter we send in the email.
if (preg_match("/Command: (.*?)\r/i", $temp, $regs) > 0) {
$info['command'] = $regs[1];
}
// Check what command we were sent. In this example, we only have one possibility.
switch ($info['command']) {
case 'OpenRPCChannel':
// Additional information is sent with this command as well, so we need to get that.
if (preg_match("/RPC-Chan: (.*?)\r/i", $temp, $regs) > 0) {
$info['rpc_key'] = $regs[1];
}
// Let's go ahead and send off a request for RPC_COMMAND1 and see if we get anything back.
$reply = sendrpc($info['rpc_key'], RPC_COMMAND1, "This is a test message.");
// Now we can check the response.
if ($reply['IntValue'] == RPC_COMMAND1) {
if ($reply['StringValue'] == 'success') {
echo "Successfully sent and received RPC_COMMAND1.\n";
}
} else {
echo "There was an error in reading the response for RPC_COMMAND1.\n";
}
break;
// You can add more command types here.
}
// Since the message was for this script and it was parsed, mark it for deletion.
imap_delete($mbox, $i);
}
}
// Close the mailbox, and delete marked emails.
imap_close($mbox, CL_EXPUNGE);
echo "Done checking mail...\n";
// Find out how long it took to run this loop.
$endtime = time();
$totaltime = $endtime - $starttime;
// Delay it a little bit if it took less than a certain amount of time to run.
if ($totaltime < MIN_LOOP_TIME) {
echo "Waiting " . (MIN_LOOP_TIME-$totaltime) . " seconds...\n";
sleep(MIN_LOOP_TIME-$totaltime);
}
echo "\n";
}
?>
Examples