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

Own default api

Started by zacekjakub, 16 June 2016 - 12:05 PM
zacekjakub #1
Posted 16 June 2016 - 02:05 PM
Hello,

could you help me a bit please? Using any http function to get some data from a webpage, I succesfully get return text of the page… Only issue is, that the returned text starts everytime with letter "?".

For example, code:


local function ktime() 
local sExample = http.get("http://minecraft.kumpacka.eu/functions/time.php") --Get contents of page
return sExample.readAll()
sExample.close()
end
returns "?time". Any idea please? :)/>

Thank you very much.
DannySMc #2
Posted 16 June 2016 - 02:40 PM
Hello,

could you help me a bit please? Using any http function to get some data from a webpage, I succesfully get return text of the page… Only issue is, that the returned text starts everytime with letter "?".

For example, code:


local function ktime()
local sExample = http.get("http://minecraft.kumpacka.eu/functions/time.php") --Get contents of page
return sExample.readAll()
sExample.close()
end
returns "?time". Any idea please? :)/>

Thank you very much.

First this goes into "Ask a Pro",
Second, you should close it BEFORE you return
local function ktime() 
	local sExample = http.get("http://minecraft.kumpacka.eu/functions/time.php") --Get contents of page
	local ret = sExample.readAll()
	sExample.close()
	return ret
end

But I can't see your PHP code, so no idea what you are doing with the PHP code, so you could be outputting the wrong thing.
TheRockettek #3
Posted 16 June 2016 - 04:05 PM
Its just printing test at the moment
KingofGamesYami #4
Posted 16 June 2016 - 06:47 PM
If you can't fix it on the PHP side, here's a workaround (this removes the first character of the string):


string.sub( sExample.readAll(), 2 )
valithor #5
Posted 16 June 2016 - 07:07 PM
In the small test i did it appears it is actually reading six characters. The first being "?" and the sixth being char with code "5" when passed to string.byte
Edited on 16 June 2016 - 09:26 PM
zacekjakub #6
Posted 17 June 2016 - 05:35 AM
In the small test i did it appears it is actually reading six characters. The first being "?" and the sixth being char with code "5" when passed to string.byte

Exatly, thank you. It is printing 6 characters, but as all of you can see, the php code returns just 5 of them.
First this goes into "Ask a Pro",
Second, you should close it BEFORE you return
local function ktime()
	local sExample = http.get("http://minecraft.kumpacka.eu/functions/time.php") --Get contents of page
	local ret = sExample.readAll()
	sExample.close()
	return ret
end

But I can't see your PHP code, so no idea what you are doing with the PHP code, so you could be outputting the wrong thing.
PHP code is not the thing, sorry but php is processed by the server, not by the client. My php script returns exactly what you see…
TheRockettek #7
Posted 17 June 2016 - 08:28 AM
Unless your php site is using POST instead of GET :)/>
DannySMc #8
Posted 17 June 2016 - 09:07 AM
Unless your php site is using POST instead of GET :)/>
This shouldn't matter, it's how he is handling the content.

In the small test i did it appears it is actually reading six characters. The first being "?" and the sixth being char with code "5" when passed to string.byte

Exatly, thank you. It is printing 6 characters, but as all of you can see, the php code returns just 5 of them.
First this goes into "Ask a Pro",
Second, you should close it BEFORE you return
local function ktime()
	local sExample = http.get("http://minecraft.kumpacka.eu/functions/time.php") --Get contents of page
	local ret = sExample.readAll()
	sExample.close()
	return ret
end

But I can't see your PHP code, so no idea what you are doing with the PHP code, so you could be outputting the wrong thing.
PHP code is not the thing, sorry but php is processed by the server, not by the client. My php script returns exactly what you see…

No the problem is, where is the random characters coming? so it might be something like an end of line or hidden character, we are trying to help you.

We ALL know it is not processed by the client, I am asking to see it in-case you are outputting something else as well, that you haven't seen, because I use HTTP and PHP to make my app store work, and I don't get any problems… So like I said, if you can show us both code, we may be able to help more.
zacekjakub #9
Posted 17 June 2016 - 02:21 PM
Thank you all! You helped me. The problem was BOM used on the file… After removing the BOM it started to work correctly.

/var/www/html/minecraft.kumpacka.eu/functions/time.php: PHP script, UTF-8 Unicode (with BOM) text

/var/www/html/minecraft.kumpacka.eu/functions/time.php: PHP script, ASCII text

I have one another question, but I will create new thread for it.
zacekjakub #10
Posted 17 June 2016 - 02:32 PM
Hello,

if I create new default API, which is supposed to be loaded by any computer by default, it works without any problems, but just in case I add it directly into the ComputerCraft.jar file. If I create new folder in default mods folder, it doesn't work at all:


 [root@minastirith apis]# pwd
/opt/minecraft/mods/ComputerCraft/lua/rom/apis
[root@minastirith apis]# ll
celkem 1
-rw-r--r-- 1 root root 5 16. čen 04.16 test

Any idea what is the reason? I am creating a lot of functions and releasing them just after every server restart is a bit slower than I would like it to be. :)/> And if I screw something up, it's also problem because I have no chance to check it after the planned automatic restart.
valithor #11
Posted 17 June 2016 - 02:44 PM
If I understand you are trying to add apis to computers by editing the jar, but when you make a folder in the mod folder to add them it doesn't work. If you are on a version of ComputerCraft that is older than version 1.55, than that method should still work, however, if you are on a newer version adding new apis is done via resource packs.

Here is a link to a tutorial for adding apis using resource packs. http://www.computercraft.info/forums2/index.php?/topic/14049-how-to-make-and-install-lua-resource-packs/

One benefit to using resource packs is that it does not require a server restart in order for computers to have the new apis, all you have to do is restart the computers themselves and the changes you made to the resource packs are then on that computer.
zacekjakub #12
Posted 17 June 2016 - 03:01 PM
If I understand you are trying to add apis to computers by editing the jar, but when you make a folder in the mod folder to add them it doesn't work. If you are on a version of ComputerCraft that is older than version 1.55, than that method should still work, however, if you are on a newer version adding new apis is done via resource packs.

Here is a link to a tutorial for adding apis using resource packs. http://www.computerc...resource-packs/

One benefit to using resource packs is that it does not require a server restart in order for computers to have the new apis, all you have to do is restart the computers themselves and the changes you made to the resource packs are then on that computer.

Thank you very much, going to try this and will let you know. :)/>
Lyqyd #13
Posted 17 June 2016 - 03:20 PM
Moved to Ask a Pro.

Threads merged. Please stick to one topic for all questions about a given program or piece of code.