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

Sending info to a real website

Started by Jasonfran, 10 November 2012 - 04:36 AM
Jasonfran #1
Posted 10 November 2012 - 05:36 AM
Is there anyway CC http functions can send info to a website and have like PHP sort it out and put it into a mysql database or something? If you can I have a brilliant idea.
remiX #2
Posted 10 November 2012 - 06:39 AM
There was someone that made a program where you can connect to a MySQL database, check the programs thread out - I can't remember the guy's name :unsure:/>/>
Orwell #3
Posted 10 November 2012 - 06:58 AM
I'm typing this on my phone so I can't test anything, but it should be like this:

sending data from CC:

local param = 42
http.post('http://example.com/script.php','param='..param)

receiving data in 'script.php' on 'example.com':

$param = $_POST['param'];
Espen #4
Posted 10 November 2012 - 07:25 AM
Yes you can send arbitrary HTTP requests (and since one of the latest CC versions even HTTPS) to a host.
That means you can feed some GET or POST data to your PHP server, let it do its thing server-side with it and then get the results back.

And I don't know if sIdEkIcK_ meant me, but I have released a working alpha version of a MySQL database connector to the peripheral section.
At the moment I'm working on implementing some updates and ideas that were suggested, as well as making it support not just MySQL but any arbitrary database managment system, as long as there exists a JDBC driver for it.

If you want to give the alpha a try, look at my sig for the link. I'd be glad to have people test it for bugs and give ideas.
Cheers

EDIT: The only thing that's not implemented yet is manipulating data though. I first wanted to see if everything worked and figured most of the time people would want to request data.
jag #5
Posted 10 November 2012 - 07:40 AM
The way the pastebin program works is that it uses the GET functionality.
So that if you do

http://www.somerandomsite.com/?this=is&a=way&to=send&info=to&a=site
(And yeah I know I declared "a" twice)
Jasonfran #6
Posted 10 November 2012 - 08:33 AM
Thanks! I know quite a lot of PHP so I have a good idea. Also is there anyway I can then send a message back to the client? Like request data, but I think that's what you said don't right?
Orwell #7
Posted 10 November 2012 - 09:10 AM
The answer would be the body of the page. So in CC:

local content = http.get('http://example.com/script.php','param='..42)
local answer = content.readAll()

and in 'script.php':

$param = $_GET['param'];
echo 'answer variable';
remiX #8
Posted 10 November 2012 - 09:25 AM
Yes you can send arbitrary HTTP requests (and since one of the latest CC versions even HTTPS) to a host.
That means you can feed some GET or POST data to your PHP server, let it do its thing server-side with it and then get the results back.

And I don't know if sIdEkIcK_ meant me, but I have released a working alpha version of a MySQL database connector to the peripheral section.
At the moment I'm working on implementing some updates and ideas that were suggested, as well as making it support not just MySQL but any arbitrary database managment system, as long as there exists a JDBC driver for it.

If you want to give the alpha a try, look at my sig for the link. I'd be glad to have people test it for bugs and give ideas.
Cheers

EDIT: The only thing that's not implemented yet is manipulating data though. I first wanted to see if everything worked and figured most of the time people would want to request data.

Yes, I meant you :unsure:/>/>
Jasonfran #9
Posted 10 November 2012 - 10:22 AM
The answer would be the body of the page. So in CC:

local content = http.get('http://example.com/script.php','param='..42)
local answer = content.readAll()

and in 'script.php':

$param = $_GET['param'];
echo 'answer variable';
Ah, but what if more than one person accessed it?
Also is there any MD5 hashing or any other type of hashing in Lua?.
Espen #10
Posted 10 November 2012 - 10:43 AM
Also is there any MD5 hashing or any other type of hashing in Lua?.
Not natively, as far as I know. Although there exist a few third party implementations.
But most of them aren't pure Lua implementations and rely on other libraries, etc.

I found one post on StackOverflow where someone posted a pure Lua implementation of MD5 (last post):
http://stackoverflow.com/questions/6083262/pure-lua-implementation-of-md5
He basically combined another, library-dependent version together with the very libraries it depends upon.
I haven't tested it, but I guess it will be rather slow.

But someone could make a peripheral that exposes a collection of all kinds of useful hashing functions.
That way you'd have the speed of Java, at the cost of having to install a peripheral.
Jasonfran #11
Posted 10 November 2012 - 10:58 AM
Also is there any MD5 hashing or any other type of hashing in Lua?.
Not natively, as far as I know. Although there exist a few third party implementations.
But most of them aren't pure Lua implementations and rely on other libraries, etc.

I found one post on StackOverflow where someone posted a pure Lua implementation of MD5 (last post):
http://stackoverflow...entation-of-md5
He basically combined another, library-dependent version together with the very libraries it depends upon.
I haven't tested it, but I guess it will be rather slow.

But someone could make a peripheral that exposes a collection of all kinds of useful hashing functions.
That way you'd have the speed of Java, at the cost of having to install a peripheral.
OK, I have been messing with some SHA1 lua libaries but I can't get them to work
Sammich Lord #12
Posted 10 November 2012 - 11:07 AM
Also is there any MD5 hashing or any other type of hashing in Lua?.
Not natively, as far as I know. Although there exist a few third party implementations.
But most of them aren't pure Lua implementations and rely on other libraries, etc.

I found one post on StackOverflow where someone posted a pure Lua implementation of MD5 (last post):
http://stackoverflow...entation-of-md5
He basically combined another, library-dependent version together with the very libraries it depends upon.
I haven't tested it, but I guess it will be rather slow.

But someone could make a peripheral that exposes a collection of all kinds of useful hashing functions.
That way you'd have the speed of Java, at the cost of having to install a peripheral.
OK, I have been messing with some SHA1 lua libaries but I can't get them to work
Try to use the StringUtills API on the forums. It has SHA1 support.
Jasonfran #13
Posted 10 November 2012 - 12:19 PM
Also is there any MD5 hashing or any other type of hashing in Lua?.
Not natively, as far as I know. Although there exist a few third party implementations.
But most of them aren't pure Lua implementations and rely on other libraries, etc.

I found one post on StackOverflow where someone posted a pure Lua implementation of MD5 (last post):
http://stackoverflow...entation-of-md5
He basically combined another, library-dependent version together with the very libraries it depends upon.
I haven't tested it, but I guess it will be rather slow.

But someone could make a peripheral that exposes a collection of all kinds of useful hashing functions.
That way you'd have the speed of Java, at the cost of having to install a peripheral.
OK, I have been messing with some SHA1 lua libaries but I can't get them to work
Try to use the StringUtills API on the forums. It has SHA1 support.
Thanks, works a charm
Orwell #14
Posted 11 November 2012 - 02:01 AM
* Snip *
Ah, but what if more than one person accessed it?

What do you mean with that?
Jasonfran #15
Posted 11 November 2012 - 04:13 AM
* Snip *
Ah, but what if more than one person accessed it?

What do you mean with that?
Well, if 2 people accessed it at the same time the PHP wont keep up and one person will get the other's result.
EDIT: Dont matter. I derped
Jasonfran #16
Posted 11 November 2012 - 06:03 AM
I need help sending more that one thing to the website.

I can send this easily

http.request("http://website.com/Read1.php", "username=" .. username)

But i want to send username and message in one request like this:


http.request("http://website.com/Read1.php", "username=" .. username, "message=" .. message)

is it possible? Because i cant seem to get it to work. If this dont work then I suppose I'd have to use http.get
Edit: Dont matter. Found out I had to put an & inbetween
Espen #17
Posted 11 November 2012 - 06:42 AM
Would this work for you?
http.request( "http://website.com/Read1.php?username="..username.."&message="..message )

Generally, you can add the first parameter to a url with ? and every consecutive one with &

Edit:
[strike]Just noticed. PHP concatenates with . and not with .. :unsure:/>/> Fixed.[/strike]
Dammit, it WAS Lua. Ok un-fixed. Thought you were talking about PHP. ^^
Edited on 11 November 2012 - 05:45 AM
Jasonfran #18
Posted 11 November 2012 - 08:28 AM
Would this work for you?
http.request( "http://website.com/Read1.php?username="..username.."&message="..message )

Generally, you can add the first parameter to a url with ? and every consecutive one with &

Edit:
Just noticed. PHP concatenates with . and not with .. :unsure:/>/> Fixed.
Dammit, it WAS Lua. Ok un-fixed. Thought you were talking about PHP. ^^
Yeah, thanks. I hopefully should have my program on the forums soon. I think it will be the first of its type
legionlabs #19
Posted 05 January 2013 - 10:29 PM
Don't know if you got that software running, but I wrote a small python HTTP server that posts/reads data to/from COSM ('internet of things' datalogging website) by stripping data from HTTP GET requests from minecraft. I mostly had hardware interfacing/networking in mind, but I guess it's the same principle. We can probably beat the script with a club until it works with whatever website you like. Send me a PM if interested.

What I have now is basically Cloud Storage for CC computers, that can be read/written by anything that can send a HTTP GET request. So, you can turn off your nuclear plant from a smartphone, or overload it using a Big Red Button (by polling things).