Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
UrlLoader POST to PHP — Gideros Forum

UrlLoader POST to PHP

rdkemptrdkempt Member
edited April 2012 in Bugs and issues
I have the following PHP file which contains the following code:

<?php echo $_POST['test']; ?>

I read the documentation for UrlLoader which seemed very straight-forward and modified the ImageLoader example app to the following:

local info = TextField.new(nil, "loading...")
info:setPosition(10, 10)
stage:addChild(info)

local function onComplete(event)
info:setText("done: " .. event.data)
end

local loader = UrlLoader.new("http://removed.com/test.php", UrlLoader.POST, "test=testing")

loader:addEventListener(Event.COMPLETE, onComplete)


When I execute this file on the Gideros Player, I expect that event.data would return 'testing', however, this is not the case. It doesn't return anything (though the TextField does say 'done: '. When I change the code on my PHP file and Lua file to use the GET method instead of POST, and change my URL, it works without a problem.

This is obviously a very basic setup but I am wondering if there is something I am missing - do I need to add additional headers to make this POST request work?

Please advise. (On an unrelated note, I did not see a way to decode JSON in the documentation - I am currently using a third party add-in, if there is a more recommended way, let me know!)

Comments

  • rdkemptrdkempt Member
    edited April 2012
    Ok, after looking into this a bit more, it is because of the Content-Type used in the HTTP header on UrlLoader POSTs (Content-Type: application/octet-stream). $_POST on my webserver will not read this, however I have temporarily setup a .htaccess file that rewrites the content-type on the folder I am test-bedding my application in to use application/x-www-form-urlencoded. I suppose something like parse_str(file_get_contents('php://input')); would work like extract($_POST); as well.

    If there is a simple way to modify the headers on POSTs in the app instead of using the hack I've implemented on my web server, that would be fantastic.
  • atilimatilim Maintainer
    Oh I see. Let me extend the UrlLoader so that it will be possible to specify the header.

    http://bugs.giderosmobile.com/issues/81

    There are some Lua codes around for decoding JSON. Honestly I don't know which one is better. But one is here: http://www.chipmunkav.com/downloads/otherdownloads.htm

    Likes: rdkempt

    +1 -1 (+1 / -0 )Share on Facebook
  • I was able to return XML data from my PHP by applying the following command just right before echoing any XML tags:

    header("Content-type: text/xml");

    The event.data then contained all the XML data I've requested with the UrlLoader. In case of another content type you should change the "text/xml" accordingly.
Sign In or Register to comment.