This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
DaGamer12345's profile picture

Php Question That's Somewhat Related To Computercraft

Started by DaGamer12345, 28 October 2013 - 06:09 PM
DaGamer12345 #1
Posted 28 October 2013 - 07:09 PM
So I have a PHP script that basically emulates rednet over the internet, but it doesn't work like it's supposed to:

<html><body>
<?php
$rednetData = $_POST['rednetData'];
list($senderid, $recipientid, $message) = explode(":", $rednetData);

if ( $recipientid == null ) {
$id = 1;
$files = scandir('/rednetFiles/');
$filecount = count($files);
for ($file = 0; $file >= 0; $filecount) {
  if (file_exists($id)) {
   $fh = fopen('/rednetFiles/{$id}', 'w');
   fwrite($fh, '{[1]={$senderid}, [2]="{$message}"}');
   fclose($fh);
   $id = $id += 1;
  }
}
} else {
$fh = fopen('{$recipientid}.txt', 'w') or die("can't open file");
fwrite($fh, '{[1]={$senderid}, [2]="{$message}"}');
fclose($fh);
}
?>
</body></html>
When it receives a post request, it doesn't do anything - the Apache logs say it received the request, but it never wrote to a file on the server. I'm pretty sure it's not the computer's end, but I'll post it's code just in case:


local tArgs = { ... }
ip = tArgs[1]
rednetDir = tArgs[2]

if #tArgs ~= 2 then
print("Usage: "..shell.getRunningProgram().." <ip> <dir of files>")
error()
end

local file = fs.open("/rednet", "w")
file.writeLine("ip = '"..ip.."'") --Writes ip
file.writeLine("rednetDir = '"..rednetDir.."'") --Writes dir

file.write[[function open( sModem ) --Function basically tells programs that the modem is opened, even if it is not there
return true
end

function close( sModem ) --Function tells programs that the modem are closed
return true
end

function isOpen( sModem ) --Function tells programs that the modem is open
return true
end

function send( nRecipient, sMessage ) --Sends a POST request to the ip
http.post(ip, os.getComputerID()..":"..nRecipient..":"..sMessage)
end

function broadcast( sMessage ) --Does the same as above, but the server will overwrite all messages
http.post(ip, os.getComputerID..":"..sMessage)
end

function receive() --GET the contents of your ID from the server [Having issues here?]
sHandle = http.get(rednetDir, os.getComputerID()..".txt")
print("Printing raw get data...")
	print(sHandle)
	print("Trying key-value printing...")
	for k, v in ipairs(sHandle) do
		print(k..", "..v)
end
	print("Printing serialized handle...")
	local tRednetData = textutils.serialize(sHandle)
local nSenderID, sMessage, nDistance = tRednetData[1], tRednetData[2], 0
os.queueEvent( "rednet_message", nSenderID, sMessage, nDistance )
return nSenderID, sMessage, nDistance
end

function run() --Wut? I do not understand the point of this but I added it from the original API anyway
if bRunning then
error( "rednet is already running" )
end
bRunning = true
receive()
end]]

file.close()

os.unloadAPI("rednet") --Reloads API
os.loadAPI("rednet") --^
print("Rednet address set to "..ip)
What could be the issue with either of the codes?
Edited on 20 November 2013 - 04:03 PM
theoriginalbit #2
Posted 28 October 2013 - 11:22 PM
Sorry I didn't comment at all, I don't know how yet in PHP and I just didn't feel like it in the client side version.
First, Comments PHP (is Google really that hard to use?!)

Either use http.post or wait for http.request to finish before continuing.

Your problem with the strings in the serialised table is you seem to have forgotten how strings work… strings are surrounded with "" not {}
DaGamer12345 #3
Posted 29 October 2013 - 03:41 PM
First, Comments PHP (is Google really that hard to use?!)

Either use http.post or wait for http.request to finish before continuing.

Your problem with the strings in the serialised table is you seem to have forgotten how strings work… strings are surrounded with "" not {}
1. I just used "didn't know how" as an excuse to not comment besides "I didn't feel like it"
2. Variables are put in braces for use in strings… Therefore the serialized table would be considered as a variable rather than part of the string. Forgot the main part of a variable is the $ symbol. My bad.
Left4Cake #4
Posted 19 November 2013 - 08:57 PM
Yeah, the biggest problem with your use of brackets is you are trying to open {$recipientid}.txt witch would look like {5}.txt and file names can't have brackets.

Also its fine to post php questions like this in the ask a pro section. Given that it is computer craft related.
DaGamer12345 #5
Posted 19 November 2013 - 09:47 PM
Yeah, the biggest problem with your use of brackets is you are trying to open {$recipientid}.txt witch would look like {5}.txt and file names can't have brackets.
So then would I drop the brackets? If not, what would I do?
Left4Cake #6
Posted 20 November 2013 - 06:28 AM
That's a start. I would also suggest looking at the results your php is sending back so you can see the errors (if any) its producing.

You can either make another html page that sends dummy $post or edit your cc program to print the http results.
DaGamer12345 #7
Posted 20 November 2013 - 07:31 AM
Would that be the only thing to drop the braces/brackets on or would I do that to other variables as well?
Left4Cake #8
Posted 20 November 2013 - 08:38 AM
From what I can tell just by looking at it that should be all you need to do.
DaGamer12345 #9
Posted 20 November 2013 - 04:58 PM
Ok, so now I get this when trying to receive stuff:
[attachment=1401:error.png]
That… shouldn't be a table, should it?
Left4Cake #10
Posted 20 November 2013 - 06:35 PM
What dose the string look like before you try to serialize it?
DaGamer12345 #11
Posted 20 November 2013 - 06:56 PM
What dose the string look like before you try to serialize it?
Well it's a table (literally, printing the handle yields a table). And when I try to print the values, there's absolutely nothing. Then it goes on to error on serialization.
Left4Cake #12
Posted 20 November 2013 - 07:47 PM
Well it's a table (literally, printing the handle yields a table). And when I try to print the values, there's absolutely nothing. Then it goes on to error on serialization.

I need to see what the table looks like before you serialize to see what wrong. It is formatted wrong some where. Just print it before serializeing it so we can take a look.
theoriginalbit #13
Posted 20 November 2013 - 08:07 PM
You do realise that so far you haven't actually read the data from what the website returned right (Lua side)?


local handle = http.get("www.google.com")
local content = handle.readAll()
handle.close()
Edited on 20 November 2013 - 07:07 PM