Jump to content

Sending and Receiving Messages from a PHP Script on my Server


Grey Warbaum
 Share

You are about to reply to a thread that has been inactive for 3526 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Hey guys! So basically, I'm creating a server that communicates through HTTP Requests with my PHP script on my website. It works fine for sending data from my object in-world to my server, and returning what the script outputs through the Body of http_response.

What I have yet to figure out how to do, is send information form my PHP script, to my object in-world, without calling an HTTPRequest in the object first.

Also, I noticed there are two different forms of:  llHTTPRequest() / http_response  and  llHTTPResponse() / http_request. Currently I'm just using the first set, in what cases is http_request / llHTTPResponse used?

Here's an example of my LSL and PHP scripts:

 

string url = "mysiteurl/databaseinterface.php";
list params = [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"];

default { state_entry()
{ request_and_release(); llHTTPRequest(url, params, "action=connect"); }
http_response(key request_id, integer status, list metadata, string body) { if(body == "Connected to Database.") { llSay(0, body);
}
}
}

 

<?php

$action=$_POST['action'];
$avatarname = $_POST['avatarname'];

if($action == 'connect') // Test Connection
{
   echo('Connected to Database.');
}

?>

 Edit: These are obviously not my full scripts, I just put these snippets in here so you could see my general syntax/flow.

 

Thanks!

Link to comment
Share on other sites

That's a huge help, thank you! Don't know why I never saw that page before.


Half my issue is solved now, I just need to know how to actually get my PHP script to send the information to my object in SL.

 


Also, if I want the message sent to multiple objects, am I going to need to store each ones URL in the php script?

Link to comment
Share on other sites

This php script is from the Kuraiko wiki page I linked to. I did add some comments.

<?php  // First we need the URL of the object/script in SL  // this is the url you get back from llRequestURL().  // It's best to store this url in your database and keep it up to date   // from SL.  $url = "http://sim10232.agni.lindenlab.com:12046/cap/f698846e-7696-beae-c0e8-4c3123fb44a6";     // Then we need something to send to the object/script in SL.  // Anything you want as long as it's a string that's shorter than 2048 bytes.  // More variables could be in a single string with "|" between them,  // and with llParseString2List() be separated in individual values again.      $msg = " Hello Second Life";      // This curl stuff is how the string gets POSTed to the object/script  header("Content-Type: text/plain");  $curl = curl_init();  curl_setopt($curl, CURLOPT_URL, $url);  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  curl_setopt($curl, CURLOPT_POST, 1);  curl_setopt($curl, CURLOPT_POSTFIELDS, $msg);  // When the curl gets executed the  llHTTPResponse() from the object/script  // gets stored in $respone  $respone = curl_exec ($curl);  curl_close ($curl);  echo $respone; // this is what comes back from the lsl script?>

Change the $url to the one your lsl script uses.

And this is the LSL script I used to try it out.

default {    state_entry() {        // Request URL        llRequestURL();    }     http_request(key id, string method, string body) {        if (method == URL_REQUEST_GRANTED) {            llOwnerSay("My URL: " + body); \\ I copied this from chat into my php script        }        else if (method == URL_REQUEST_DENIED) {            llSay(0, "Something went wrong, no url. " + body);        }        else if (method == "POST") {            llOwnerSay("\nReceived POST Request:" + body);                         //Respone (what you see in your browser)            llHTTPResponse(id,200,"success");        }        else {            llHTTPResponse(id,405,"Unsupported Method");        }    }}

 

I hope this helps. 



  • Like 1
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3526 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...