[SOLVED] Get Output from Webpage

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

[SOLVED] Get Output from Webpage

Post by TheMeq »

Hi all, I'm trying to use the tutorial on the wiki for lure to get output from a webpage, but it looks like the wiki page is outdated, and there doesn't appear to be any up to date tutorials or documentation for the later versions of lure (like the one from the github).

I essentially just need to read a string of text from a webpage, something like this:

Code: Select all

response = query("http://www.domain.com","a=1&b=2","GET")
response = "Hello"
Is there a way to do this?
Last edited by TheMeq on Thu Oct 29, 2015 11:43 am, edited 1 time in total.
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Get Output from Webpage

Post by bobbyjones »

I think you can use luasockets http module but it will return the html and what ever else is in the page.
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

Re: Get Output from Webpage

Post by TheMeq »

Got what I needed with the following, just incase anyone else wants it. Please note, it is blocking, my next step is to figure out how to make it non-blocking, but for the game I'm developing, it's not too much of an issue. :D

Code: Select all

http = require("socket.http")
ltn12 = require("ltn12")

function request(urlget)
    local result_table = {}
    client,r,c,h = http.request{
    url = urlget,
    sink = ltn12.sink.table(result_table)}
    local result = table.concat(result_table)
    return result
end

function love.load()
    b = request("http://www.themeq.xyz/ethereal/api.php")
    a = "Hello"
    if a == b then
        print("Matched!")
    else
        print("Not Matched!")
    end
    print(b)
end
Enjoy :D
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: [SOLVED] Get Output from Webpage

Post by bobbyjones »

I have a non-blocking variant that uses bartbes' async lib if you want it.
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

Re: [SOLVED] Get Output from Webpage

Post by TheMeq »

If you could share, that would be awesome!
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: [SOLVED] Get Output from Webpage

Post by bobbyjones »

These are the two relevant files. My utils.request is like your http wrapper thing. and in Todo.lua all I do is require async and add the requests as jobs. When you require async it will pass around one instance. So you do not need to worry about it being a global.

https://github.com/Bobbyjoness/To-Do-Ap ... /utils.lua
https://github.com/Bobbyjoness/To-Do-Ap ... s/Todo.lua
https://github.com/Bobbyjoness/To-Do-Ap ... r/main.lua
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests