network.request returns response without data in PHP

Hi there,

I am running into problems with my server communication.

My network.request command runs without any problem in the simulator and even returns a response 

in the networkListener.

The response shows that the simple string I sent to the server should be in the array server-side, but after checking, the array is empty.

My question now is why do I get a positive response from my networkListener, when there is no data in the array. How does the PHP script need to look like?

[lua]

– NETWORK

local function networkListener(event)

    if ( event.isError ) then

        print( “Network error!” )

    else

        print( "RESPONSE: "…event.response )

    end

end

local function sendToServer()

    local headers = {}

    headers[“Content-Type”] = “application/x-www-form-urlencoded”

    headers[“Accept-Language”] = “en-US”

    

    local params = {}

    params.headers = headers

    params.body = “message=hey”

    network.request( “http://exampleURL”, “POST”, networkListener, params)

end

[/lua]

Google “PHP empty POST array” and you will get a ton of hits about this issue. If your data is under 2K, perhaps use GET instead of POST.

Rob

i would change the way you send to the server in json format:

local headers = { ["Content-Type"] = "application/json", ["Accept-Language"] = "en-US", } local sendInfo={ ["message"]="hey", } local params = {} params.headers = headers params.body = json.encode( sendInfo ) }) local request=network.request( url, "POST", networkListener, params)

in server side you just need this:

$data = file\_get\_contents('php://input'); $json = json\_decode($data, true); $info = $json["message"];

$info should have the “hey” string.

Hi Rob, 

How to solve this problem if server give Forbidden message on Lua GET command. 

Can you post the new code and copy/paste the error you’re getting?

What happens when you try to access the URL in a web browser?

Ok, as of right now no progress made…

I tried using different headers and making the php script look for those. I will 

post the code of both down below:

PHP

[lua]

<?php

if(empty($_SERVER[‘CONTENT_TYPE’])){

     $type = “application/x-www-form-urlencoded”;

     $_SERVER[‘CONTENT_TYPE’] = $type;

}

$data = file_get_contents(‘php://input’);

$json = json_decode($data, true);

$info = $json[“message”];

if (empty($info))

{

echo “empty”;

}

else

{

echo $info;

}

?>

[/lua]

Corona

[lua]

local headers = {

    [“Content-Type”] = “application/x-www-form-urlencoded”, 

    [“Accept-Language”] = “en-US”,

    }

    local sendInfo={

        [“message”]=“hey”,

    }

    local params = {}

    params.headers = headers

    params.body = json.encode( sendInfo )

    local request = network.request( “http://localhost/testMessage.php”, “POST”, networkListener, params)

[/lua]

the message in the browser is still empty, however I always get a response in the 

console in corona, Saying: “Response: hey”

Like it should be online.

change your headers to:

local headers = { ["Content-Type"] = "application/json", ["Accept-Language"] = "en-US", }

Thanks for your help, but unfortunately it still doesn’t work :confused:

Like is there something I may missed in a config file, or is it even possible 

to send information with the Corona Simulator?

Is there anything else I can try?

What I actually need is sending a json table to a php server, which then saves it into

a mySQL database, as well as a way to access this data from a device through the

application. Ideally I’d like to use a POST call.

As I said, I get a response which contains the array in the simulator consol, but the array server side is always empty…

Thanks for your help guys!

Google “PHP empty POST array” and you will get a ton of hits about this issue. If your data is under 2K, perhaps use GET instead of POST.

Rob

i would change the way you send to the server in json format:

local headers = { ["Content-Type"] = "application/json", ["Accept-Language"] = "en-US", } local sendInfo={ ["message"]="hey", } local params = {} params.headers = headers params.body = json.encode( sendInfo ) }) local request=network.request( url, "POST", networkListener, params)

in server side you just need this:

$data = file\_get\_contents('php://input'); $json = json\_decode($data, true); $info = $json["message"];

$info should have the “hey” string.

Hi Rob, 

How to solve this problem if server give Forbidden message on Lua GET command. 

Can you post the new code and copy/paste the error you’re getting?

What happens when you try to access the URL in a web browser?

Ok, as of right now no progress made…

I tried using different headers and making the php script look for those. I will 

post the code of both down below:

PHP

[lua]

<?php

if(empty($_SERVER[‘CONTENT_TYPE’])){

     $type = “application/x-www-form-urlencoded”;

     $_SERVER[‘CONTENT_TYPE’] = $type;

}

$data = file_get_contents(‘php://input’);

$json = json_decode($data, true);

$info = $json[“message”];

if (empty($info))

{

echo “empty”;

}

else

{

echo $info;

}

?>

[/lua]

Corona

[lua]

local headers = {

    [“Content-Type”] = “application/x-www-form-urlencoded”, 

    [“Accept-Language”] = “en-US”,

    }

    local sendInfo={

        [“message”]=“hey”,

    }

    local params = {}

    params.headers = headers

    params.body = json.encode( sendInfo )

    local request = network.request( “http://localhost/testMessage.php”, “POST”, networkListener, params)

[/lua]

the message in the browser is still empty, however I always get a response in the 

console in corona, Saying: “Response: hey”

Like it should be online.

change your headers to:

local headers = { ["Content-Type"] = "application/json", ["Accept-Language"] = "en-US", }

Thanks for your help, but unfortunately it still doesn’t work :confused:

Like is there something I may missed in a config file, or is it even possible 

to send information with the Corona Simulator?

Is there anything else I can try?

What I actually need is sending a json table to a php server, which then saves it into

a mySQL database, as well as a way to access this data from a device through the

application. Ideally I’d like to use a POST call.

As I said, I get a response which contains the array in the simulator consol, but the array server side is always empty…

Thanks for your help guys!