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

[HTTP/PHP] Calling PHP functions with http.post

Started by ComputerCraftFan11, 04 August 2012 - 06:57 AM
ComputerCraftFan11 #1
Posted 04 August 2012 - 08:57 AM
Hello, I got a php database api thing and it has a function called upload(name, data)

How can I run that function?
This is what I tried:

http.post("http://ccnet.ddns.me/php/core.php", "upload(\"test\", \"print(\"Hello world!\")\")")

(I got this from pastebin)
But it didn't work, does anyone know how?
Xtansia #2
Posted 04 August 2012 - 11:34 AM
This topic may be of some use to you http://www.computercraft.info/forums2/index.php?/topic/2915-saving-scores-on-a-website
ComputerCraftFan11 #3
Posted 04 August 2012 - 08:14 PM
This topic may be of some use to you http://www.computerc...es-on-a-website

It doesn't show functions. Someone (samrg) gave me a php code with this:

    protected function upload($name, $data){
	    if($this->Write_Lock){
		    die("Write_lock");
	    }
	    $handle = fopen($this->UploadDirectory . basename($name), "w");
	    fwrite($handle,$data);
	    fclose($handle);
	    print("success");
    }
ComputerCraftFan11 #4
Posted 04 August 2012 - 10:40 PM
bump :C

Does anyone know how?
xuma202 #5
Posted 04 August 2012 - 10:52 PM
Yes you can't you'll have to write the PHP script so that it reads the parameters from the $_POST and $_GET array and make it act accordingly
cant_delete_account #6
Posted 05 August 2012 - 03:13 AM
bump :C

Does anyone know how?
Lua code:

local function post(one,two)
		http.post(
				"http://website.extension/folder/post.php",
				"one="..textutils.urlEncode(tostring(one)).."&"..
				"two="..textutils.urlEncode(tostring(two))
		)
end
post("testing", "123")
PHP code:

<?php
if(isset($_POST['one']) &amp;&amp; isset($_POST['two'])){
		$one = urldecode($_POST['one']);
		$two = urldecode($_POST['two']);
		echo "One: ".one."nTwo: ".two;
}
?>
xuma202 #7
Posted 05 August 2012 - 10:16 AM
Well this is already pretty good.

Now you can make an if/switch-case block in PHP that compares $one with possible values and then executes the function.
Another way is using this http://php.net/manual/en/function.eval.php function with $one / $two as a parameter.

Btw: I guess you made a little mistake here:


"one="..textutils.urlEncode(tostring(two)).."&amp;"..
						    "two="..textutils.urlEncode(tostring(one))
one = two
two = one ?????
cant_delete_account #8
Posted 05 August 2012 - 06:16 PM
Well this is already pretty good.

Now you can make an if/switch-case block in PHP that compares $one with possible values and then executes the function.
Another way is using this http://php.net/manua...nction.eval.php function with $one / $two as a parameter.

Btw: I guess you made a little mistake here:


"one="..textutils.urlEncode(tostring(two)).."&amp;"..
							"two="..textutils.urlEncode(tostring(one))
one = two
two = one ?????
Oops. Fixed.
xuma202 #9
Posted 05 August 2012 - 07:29 PM
Wow @thesbros I thought your first post was made by ComputerCrfatFan11