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

Help using http to save data on remote servers

Started by MayContainVennom, 12 March 2013 - 04:57 AM
MayContainVennom #1
Posted 12 March 2013 - 05:57 AM
Hello there, I am hoping that someone can help me with a code problem I am having:

Code Background:

I started a server with one of my freinds (ckriskross09c). after watching one of Guude's FTB server videos I diced to manipulate a code that would I could use to open a door with a way to change the length of the door open time and something different. I used the Advanced monitors and a code that would change the background colour and open a door. The good thing for me was that I didn't have to to a lot of work to make it work.
(For reference I have included the code bellow)
Spoilerm= peripheral.wrap("back")
color = 1
m.setBackgroundColor(color)
while true do
waiting = os.pullEvent(monitor_touch)
if (color < 32768) then
color = color + color
else
color = 1
end
m.clear()
m.setBackgroundColor(color)
end

–Then I simply added a a Redstone output and a sleep.

After we started to get some resources in I diced to add the Misc peripheral's Player detector. this would allow me to make the house more "safe" also it allowed me to mess with the coding more :D/> in the house I also have a Direwolf20 monitor that I was using as a control panel.

Yesterday I started to develop a small amount of Rednet into the mix making all the 'Door controllers' to feed to a computer that could display who was using that door.

Receiver program:
Spoilerm= peripheral.wrap("back")
color = 1
m.setBackgroundColor(color)
while true do
waiting = os.pullEvent(monitor_touch)
if (color < 32768) then
color = color + color
else
color = 1
end
m.clear()
m.setBackgroundColor(color)
end

A example of the sender program using the Player detector:
Spoiler–Player Detector door Research from Guude
p = peripheral.wrap("front")
rednet.open("left")
a, b = os.pullEvent("player")
if (b == "MayContainVennom") or (b == "ckriskross09c") then
print("It works!")
rednet.send(32,b.." Opened Test door")
redstone.setOutput("left",true)
sleep(5)
os.reboot()
else
print("D:")
rednet.send(32,b.." Tried to open Test door")
os.reboot()
end

My question:

I have all these codes on Pastebin. It would be awesome if I ever start using these codes on another server. if so it would be even better if I could get the computers to use the HTTP API and save the Person using the device to be logged so I can look back at it later. know this is my problem. I cant get my head around the HTTP API. so if someone could be kind enough to help me in this problem. Also if it could be uploaded to somewhere like Google docs that would make my life easier. I do have a computer that can host a SQL server so there is no problem there. hut I would prefer to have it in somewhere like google so I don't have to be putting my Ip on loads of servers.

Thank you for reading and I hope you can help me. if you prefer to help me vocally my skype is: s3an1997 if you have any other questions ask away!
Lyqyd #2
Posted 12 March 2013 - 06:29 AM
Split into new topic. You did not provide a title, so a title was provided for you.
MayContainVennom #3
Posted 12 March 2013 - 06:33 AM
Thanks

Split into new topic. You did not provide a title, so a title was provided for you.

Thanks
Bubba #4
Posted 12 March 2013 - 07:11 AM
Well you should check out the build-in program "pastebin", which can be find under .minecraft\mods\ComputerCraft\lua\rom\programs\http if you have unzipped the ComputerCraft mod.

I'm not sure if you've encountered the wiki, but it is an excellent resource which you should look into. If you have and are still having issues, please be more specific about what it is you're having trouble with.

I do have a computer that can host a SQL server

Unfortunately there is no way to directly interact with an SQL server from vanilla ComputerCraft, although there is this for CC 1.481. For vanilla CC you can create a "proxy" of sorts through the language of your choice so long as it can accept post/get data.

PS - It's nice to see someone with good grammar, spelling, and manners asking questions. You have no idea how much I appreciate this XD
MayContainVennom #5
Posted 12 March 2013 - 07:18 AM
Well you should check out the build-in program "pastebin", which can be find under .minecraft\mods\ComputerCraft\lua\rom\programs\http if you have unzipped the ComputerCraft mod.

I'm not sure if you've encountered the wiki, but it is an excellent resource which you should look into. If you have and are still having issues, please be more specific about what it is you're having trouble with.

I do have a computer that can host a SQL server

Unfortunately there is no way to directly interact with an SQL server from vanilla ComputerCraft, although there is this for CC 1.481. For vanilla CC you can create a "proxy" of sorts through the language of your choice so long as it can accept post/get data.

PS - It's nice to see someone with good grammar, spelling, and manners asking questions. You have no idea how much I appreciate this XD

Yeah that would work fine on a server I host but if it is a public server then I have a problem. What is the default way the http API talks to?

And thanks for the compliment :P/>
remiX #6
Posted 12 March 2013 - 07:30 AM
Well, if you know some PHP you could use a free website hosting server and create two php pages:
One that allows you to add data into a php page
And another that displays all information.

This way you can use CC to send information to add data so it will be added to a database - this is exactly what I do with my ComputerCraft YouTube program.
And then you can also use CC to retrieve the information from a website that displays all the information from a database - this is also what I do for my CC YouTube program.

What do you mean by 'What is the default way the http API talks to?'
Bubba #7
Posted 12 March 2013 - 07:32 AM
Edit: You ninja powers mean nothing to me RemiX!

Yeah that would work fine on a server I host but if it is a public server then I have a problem. What is the default way the http API talks to?

And thanks for the compliment :P/>

Not sure what you mean by the default way the http API talks to. By proxy I mean a proxy located on the server using a language such as PHP. Here would be an example of accepting and posting POST data:

Example site:

<?php
$info = $_POST["name"];
if (isset($info)) {
  echo "Hello there, " . $info;
} else {
  echo "Invalid post. Requires some data.";
}
?>

And the Lua code:

local url = "http://www.example.com/mypage.php"
local name = "Bob"
local sRespone = http.post(url, "name="..name)
if not sResponse then
  print("Issue contacting server")
else
  print(sResponse)
end

From this you could modify the PHP program to accept data that it inserts into an SQL database. You should probably have the user enter a password that is authenticated by the server to keep malicious code out of your server :)/>
MayContainVennom #8
Posted 12 March 2013 - 08:01 AM
*Removed*
MayContainVennom #9
Posted 12 March 2013 - 09:14 AM
Could you please direct me to a suitable PHP server or something that can get me started (Like a video) this has both given me and my friend a headache trying to work it out.
MayContainVennom #10
Posted 12 March 2013 - 10:55 AM
OKay we have set up the php server know we face this problem: of the error saying Issue contacting the server.
I am using a diffrent network to where the website is hosted this is how we have edited it but it wont work:
SpoilerRemove due to ip

but we do get this responce from the server:
SpoilerRemove due to ip

Help!
Bubba #11
Posted 12 March 2013 - 11:48 AM
OKay we have set up the php server know we face this problem: of the error saying Issue contacting the server.
I am using a diffrent network to where the website is hosted this is how we have edited it but it wont work:
Spoilerhttp://imgur.com/LCfv1kJ

but we do get this responce from the server:
Spoilerhttp://prntscr.com/vy3z9

Help!

Your Lua code is wrong. http.post takes two arguments: the url and the post-data. Post data is formatted like so: var=value&amp;secondvar=anothervalue etc. etc.

I don't know exactly what your PHP code is, but assuming that you are trying to capture $_POST["name"] then you would do something like this:


local url = "yourUrl.com/index.php"
local name = "Bob"
local sResponse = http.post(url, "name="..name)
if not sResponse then
  print("Issues contacting server")
else
  print(sResponse)
end
MayContainVennom #12
Posted 13 March 2013 - 04:19 AM
Now I'm getting this:
Spoilerhttp://prntscr.com/w0zv2
remiX #13
Posted 13 March 2013 - 04:25 AM
try changing
print(sResponse)
to
print(sResponse.readAll())
and then sResponse.close()
Sammich Lord #14
Posted 13 March 2013 - 04:27 AM
I have not been ninja'd since I was watching a movie.
It returns a table.
So you have to print all the data you received, like so:

response = http.get("url.tld")
print(response.readAll())
MayContainVennom #15
Posted 13 March 2013 - 04:41 AM
Now it is coming up saying attemp to index ? (a nil value)
Bubba #16
Posted 13 March 2013 - 05:24 AM
Now it is coming up saying attemp to index ? (a nil value)

I believe that is because it cannot contact the webserver properly. Did you check to make sure that sResponse is not nil? Please include your code (minus the web address if you prefer and are sure that it is valid) so that we can properly debug it.
MayContainVennom #17
Posted 13 March 2013 - 05:36 AM
Removed due to ip
Bubba #18
Posted 13 March 2013 - 05:50 AM
responce = http.get(yourUrl)

Should be "response" instead.
remiX #19
Posted 13 March 2013 - 06:09 AM
I have not been ninja'd since I was watching a movie.
It returns a table.
So you have to print all the data you received, like so:

response = http.get("url.tld")
print(response.readAll())

Hehe ;)/>

Are you using a php script on your local pc?
MayContainVennom #20
Posted 13 March 2013 - 08:54 AM
http://prntscr.com/w26ht And I am running on a different Computer
remiX #21
Posted 13 March 2013 - 09:00 AM
http://prntscr.com/w26ht And I am running on a different Computer

Can't see a thing from that?
Why'd you upload it?
What's the latest error?

You keep posting pictures with no explanation…
Bubba #22
Posted 13 March 2013 - 09:28 AM
-snip-
MayContainVennom #23
Posted 13 March 2013 - 10:29 AM
http://prntscr.com/w26ht And I am running on a different Computer

Can't see a thing from that?
Why'd you upload it?
What's the latest error?

You keep posting pictures with no explanation…

Wondering why it is coming up with the posting error when It is the same as Bubba had it
Bubba #24
Posted 13 March 2013 - 11:43 AM
http://prntscr.com/w26ht And I am running on a different Computer

Can't see a thing from that?
Why'd you upload it?
What's the latest error?

You keep posting pictures with no explanation…

Wondering why it is coming up with the posting error when It is the same as Bubba had it

Because either I or you may have an error. Perhaps you have a spelling mistake just like the last time. Please just post the code on pastebin (it is not that hard - just copy/paste).
MayContainVennom #25
Posted 14 March 2013 - 04:52 AM
Okay, here is the code:
SpoilerRemoved link
Bubba #26
Posted 14 March 2013 - 07:07 AM
Change it to this:


local url = "website"
local name = "Bob"
local sResponse = http.post(url, "name="..name)
if not sResponse then
  print("Issues contacting server")
else
  print(sResponse.readAll())
  sResponse.close()
end
MayContainVennom #27
Posted 14 March 2013 - 07:45 AM
Okay update time!

With that code I have managed to get the computer craft side to work ( :D/>) I am getting a response saying 'Hello there, Bob')

Now for the bad news no data has been sent to the site. ( :L)

Just as a side note This a map of the computer craft side :
http://imgur.com/wBgQWn0


(The PHP is setup the same way you posted it using WAMP)
Bubba #28
Posted 14 March 2013 - 08:15 AM
What do you mean no data is being sent to the site? The username (Bob) is being sent, although to save it you would need to do some extra php coding. What exactly are you looking for this to do?
MayContainVennom #29
Posted 14 March 2013 - 08:39 AM
Save the data. But yeah we got talking to the website working :D/>

Could you also help me work it in from the message that I get in, to what I have to send out? I just need the http.send line Thanks :D/>
MayContainVennom #30
Posted 15 March 2013 - 08:51 AM
As an update Me and my freind (and his dad) have manged to make it work! I'm going to remove all the posts that have my ip and if requested I wll upload all the codes Ip less :D/> Thanks or all the help.