[RESOLVED - Yay] I can't get a super basic HTTP POST network request to work

I looked at as many relevant posts I could find, but I still can’t workout why my PHP side isn’t getting the data I’m sending from the Simulator (using OSX).

Goal: Use an asynchronous HTTP call to make a network POST request to PHP from the Simulator, and have it send the POST’ed data back to the Simulator. (Assumption: If it works on Simulator, it would work on iOS device.) (Edit: I’m suing daily build 840.)

-- LUA side:  
local myURL = "http://www.myWeb.com/testNetwork.php"  
local function myRequest(set\_email, set\_user)  
 local function networkListener( event )  
 if ( event.isError ) then  
 print( "Network error!")  
 print ( "event.isError = ", event.isError )  
 print ( "event.url = ", event.url )  
 print ( "event.name = ", event.name )  
 print ( "event.response = ", event.response )  
 print ( "event.response = ", event.status )  
 else  
 print ( "RESPONSE: " .. event.response )  
 end  
 end  
 local param = {}  
 param.body = "email=set\_email&username=set\_user"  
 network.request( myURL, "POST", networkListener, params)  
end  
myRequest(myEmail, myUsername)  
  
-- PHP side:  
<?php <br> $email = $\_POST['email'];  
 $username = $\_POST['username'];  
 $result = array();  
 $result["result"] = 'Received';  
 $result["message"] = 'The email address we received is ' . $email . ' and username received is ' . $username;  
 echo json\_encode($result);  
?\>  
  
-- The terminal prints the following  
RESPONSE: {"result":"Received","message":"The email address we received is and username received is "}  

What I’m trying to do is so simple, and it’s pitiful that it refuses to work for me. I really want to be able to do POST (and eventually PUT), but I don’t seem to be able to make this very first baby step. What am I doing wrong?

Here are some reading I’ve done, but I’m still not getting it:
http://docs.coronalabs.com/api/library/network/request.html#examples
http://developer.coronalabs.com/forum/2011/10/11/posting-data-web-server
http://developer.coronalabs.com/forum/2012/04/03/networkrequest
http://developer.coronalabs.com/code/validation-auto-renewable-app-purchases
http://developer.coronalabs.com/forum/2012/07/15/hide-php-echo-being-browser-viewable
http://developer.coronalabs.com/forum/2012/07/19/our-server-can-not-receive-post-data-https
http://developer.coronalabs.com/forum/2012/04/16/tutorial-how-make-your-app-talk-web-service-or-how-setup-web-service-your-app-talk

Any help would be so appreciated.

Naomi
[import]uid: 67217 topic_id: 29545 reply_id: 329545[/import]

@Naomi
In my opinion, if u can get a response like this

RESPONSE: {“result”:“Received”,“message”:"The email address we received is and username received is "}

it means your server can receive request from your Simulator

U should check your PHP side
check your parameter “username” and “email”.
Do they set value correctly?
Try print some log on your PHP side if it’s possible. [import]uid: 25057 topic_id: 29545 reply_id: 118572[/import]

@OwenYang, thank you for your reply post. My PHP side is exactly what I posted above. Nothing fancy. Very simple. All it does is receive the param and give it right back to $result array. I figure, I need to strip everything down to the very basics so that I understand what’s going on, and every step of the way. Do you see anything wrong with the PHP code?

I don’t know how to “print some log” on my PHP side. (I just recently started learning the basics of PHP, and I’m still so new to this.)

Thanks again for all your help.

Naomi [import]uid: 67217 topic_id: 29545 reply_id: 118573[/import]

By the way, I also set up a web form using PHP, and I can enter email & username directly from the web form. I use $_POST to retrieve the user input. I can even insert the email address & username directly into MySQL database. So, in terms of the server side, I know it works. I also know that the PHP can handle the email / username input properly.

It almost feels like as if the network POST request sent by Corona Simulator isn’t really sending the email / username data to PHP at all.

I’ve seen posts on Forum that reports the similar situation, but I am yet to see the solution that I can implement and would work with my situation.

Naomi

Edit: @OwenYang, I’ll give your suggestion a try and post back. Thank you so much for taking the time to write up the code. I really appreciate it. (And I really want to be able to work with POST and PUT.) [import]uid: 67217 topic_id: 29545 reply_id: 118575[/import]

if u use GET u should pass parameter in your “URL”
like this

local param = {}  
param.body = ""  
network.request( myURL.."?email=set\_email&username=set\_user", "GET", networkListener, params)  

then get parameter on PHP side (i don’t know PHP well )
try it :slight_smile:

if u use “POST” method
i think u can do either of below
(1) set attribute ‘email’ and ‘username’
( i don’t know how to set attribute in corona…, i use json to communicate with server, try json)
(2) use json

local userJson = {}  
  
 userJson .username = set\_user  
 userJson .email = set\_email  
  
 local function networkListener( event )  
 if ( event.isError ) then  
-- native.showAlert("error","error",{"ok"})  
 else  
 print ( "RESPONSE" .. event.response )  
 end  
 end  
 local headers = {}  
  
 headers["Content-Type"] = "application/json; charset=utf-8"  
 headers["Accept-Language"] = "en-US"  
  
 local params = {}  
 params.headers = headers  
 params.body = json.encode(userJson )  
 network.request( myURL, "POST", networkListener, params)  

all u have to do is try do parse json and get value in PHP .:slight_smile:
see it if this help
http://www.php.net/manual/en/function.json-decode.php
good luck
[import]uid: 25057 topic_id: 29545 reply_id: 118574[/import]

Hi .Naomi .Hi ,OwenYang
I want to send my Device ID (Device Token ) to PHP server Could you help me to do this??? [import]uid: 160777 topic_id: 29545 reply_id: 118577[/import]

@Naomi
i think the main reason u can’t get email and username
is that in corona side it didn’t set “attribute ‘email’ ans ‘username’”
(it did in web form side)
so while u try to get

 $email = $\_POST['email'];  
 $username = $\_POST['username'];  

i guess u will get nothing(null or blank)
Maybe u can try another way
like
get body content
then u will get text like this “email=set_email&username=set_user”
however u have to exact value on your own. [import]uid: 25057 topic_id: 29545 reply_id: 118578[/import]

@OwenYang, you are right in that PHP side is getting nothing (null or blank) even when I’m sending the body content text to “email=set_email&username=set_user”. I even hard coded set_email to “myEmail” and set_user to “myUserName”, and still it’s getting the same result.

I tried the json route as you suggested (and I do need to be able to use json encode/decode once I get over the initial hardle), and here’s the code I used:

-- LUA side:  
local myURL = "http://www.myWeb.com/testNetwork.php"  
local function myRequest(set\_email, set\_user)  
 local function networkListener( event )  
 if ( event.isError ) then  
 print( "Network error!")  
 print ( "event.isError = ", event.isError )  
 print ( "event.url = ", event.url )  
 print ( "event.name = ", event.name )  
 print ( "event.response = ", event.response )  
 print ( "event.response = ", event.status )  
 else  
 print ( "RESPONSE: " .. event.response )  
 end  
 end  
 local userJson = {}  
 userJson.username = set\_user  
 userJson.email = set\_email  
 local headers = {}  
 headers["Content-Type"] = "application/json; charset=utf-8"  
 headers["Accept-Language"] = "en-US"  
 local param = {}  
 param.headers = headers  
 param.body = json.encode(userJson)  
 network.request( myURL, "POST", networkListener, params)  
end  
myRequest("myEmail", "myUsername")  
  
-- PHP code  
<?php <br> $userdata = array();  
 $userdata = $\_POST['userJson'];  
 $userdata = json\_decode($userdata);  
  
 $result = array();  
 $result = $userdata;  
 echo json\_encode($result);  
?\>  
  
-- The terminal prints the following  
RESPONSE: null  

I’m getting null!!! Why isn’t my PHP getting the POST data from Corona?

Naomi
[import]uid: 67217 topic_id: 29545 reply_id: 118580[/import]

Hey there,

This works for me I believe, maybe give it a try as a starting point.

<?php <br> $email = $\_GET['email'];  
 $username = $\_GET['username'];  
  
 $result = json\_encode  
 (  
 array(  
 'email' =\> $email,  
 'username' =\> $username,  
 )  
 );  
   
 echo $result;  
?\>  

it sets things up a little differently than what you have. As you know I’m a newb (TOTAL in PHP) and have no idea why what you have doesn’t work.

Nail [import]uid: 106779 topic_id: 29545 reply_id: 118582[/import]

@Naomi
Do not use $userdata = $_POST[‘userJson’];
try one of these
(1)

$post = http\_get\_request\_body()  

http://www.php.net/manual/en/function.http-get-request-body.php
(2)

$post = file\_get\_contents('php://input');  

http://stackoverflow.com/questions/8945879/how-to-get-body-of-a-post-in-php
@xnailbender
thanks :slight_smile:
i think it works when use “GET” method [import]uid: 25057 topic_id: 29545 reply_id: 118583[/import]

@dev_wp7
u can use it in your corona side
(edit from Naomi’s code )

local myURL = "" -- add here  
local function myRequest()  
 local function networkListener( event )  
 if ( event.isError ) then  
 else  
 print ( "RESPONSE: " .. event.response )  
 end  
 end  
 local param = {}  
 param.body = system.getInfo("deviceID")  
 network.request( myURL, "POST", networkListener, params)  
end  
myRequest()  

php side code

[code]

<?php
$deviceid = http\_get\_request\_body() // then add device id into your database.... ?\> [/code] [import]uid: 25057 topic\_id: 29545 reply\_id: 118585[/import]

@xnailbender, I’m totally new with PHP (only 2 weeks into learning it), so I’m way behind you. About the GET method, I’m trying to learn how to use POST and minimize the use of GET. I’m probably coming from this fear stemming from ignorance, but it says GET should not be used for operations that cause side-effects (using it for actions in web applications is a common mis-use): http://en.wikipedia.org/w/index.php?title=Hypertext_Transfer_Protocol&oldid=101414787

So, if I can, I want to work out how I may send & receive POST requests.

Edit:
@OwenYang, both of the following returned null (or blank) response:

-- attempt one  
<?php <br>$userdata = http\_get\_request\_body()  
echo $userdata;  
?\>  
-- terminal output:  
RESPONSE:  
  
-- attempt two  
<?php <br>$userdata = file\_get\_contents('php://input');   
echo $userdata;  
?\>  
-- terminal output:  
RESPONSE:  

It seems like I have a lot more PHP learning to do…

Thanks again!

Naomi [import]uid: 67217 topic_id: 29545 reply_id: 118586[/import]

@Naomi
sorry i have no idea why it got null in PHP.

But i can make sure one thing ,
corona side code is correct cause i use it in my APP.
it works fine.

PS.server side i use JSP.
so i think it must work in PHP. just have to find ways to read request body.

GOOD LUCK
Owen [import]uid: 25057 topic_id: 29545 reply_id: 118587[/import]

@OwenYang, thank you for letting me know. I will need to learn how to receive Corona data via PHP – and then I also need to be able to send data back to Corona using PHP. It sounds like there’s still a long road to trudge along. *sigh*

I wonder if there’s any PHP sample code that does the VERY BASIC communication with Corona app – that uses POST and PUT method. I mean, really stripped down version. Nothing fancy. Just plain simple. That’s all I need. It would really help…

Naomi

Edit: Thanks to Robmiracle and many others posts on forum, I am able to get the version working when I use “GET” method using both json and base64 encoding. But honestly, I’d like to know how I could do the same thing with POST and PUT method. Can someone post some very very basic PHP code to achieve this, please? [import]uid: 67217 topic_id: 29545 reply_id: 118589[/import]

@xnailbender, you noted that you got header thing working: http://developer.coronalabs.com/forum/2012/07/15/hide-php-echo-being-browser-viewable#comment-115621

Assuming you are using HTTP POST method, may I ask if you could share some stripped down version of the LUA and PHP scripts that enable communication between Corona app and PHP? Sending a couple of string data, and returning the very same data back from the PHP to Corona is all I’m asking… It would be so helpful…

Naomi [import]uid: 67217 topic_id: 29545 reply_id: 118594[/import]

Hey Naomi, I don’t have what you’re looking for. I’ve been working on it for a couple hours and, like you, can’t seem to POST the data out of a param. I’ve tried about everything and every example I see is a basic… $username = $_POST[‘postData’]

I have been using POST, but have the GET variables I use are added onto the end of the URL, which I now see wouldn’t work to well when passing usernames and passwords.

Hoping Rob or Joakim will give us a heads up, it could just be a simple sytax error. I don’t see any reason why the solutions posted don’t work

Arrggghhh!

Nail

[import]uid: 106779 topic_id: 29545 reply_id: 118626[/import]

I actually switched to using a python webserver because I was so annoyed with getting PHP and POST working the way i wanted -_- [import]uid: 147305 topic_id: 29545 reply_id: 118669[/import]

@OwenYang
Thnx 4 ur help ,i appreciate that :slight_smile: [import]uid: 160777 topic_id: 29545 reply_id: 118713[/import]

I am beginning to wonder if there’s a problem with Corona’s HTTP POST implementation. Has anyone successfully implemented network request using HTTP POST method? (As opposed to the HTTPS POST method.)

Naomi [import]uid: 67217 topic_id: 29545 reply_id: 118772[/import]

Well, as mentioned above I have, both on 840 and i think im on 868 now. Only problem is I didn’t use PHP so really it only tells you it isn’t a corona issue. [import]uid: 147305 topic_id: 29545 reply_id: 118781[/import]