Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

[RESOLVED - Yay] I can't get a super basic HTTP POST network request to work
Started by Naomi Aug 06 2012 10:20 AM

- - - - -
47 replies to this topic
[TOPIC CONTROLS]
Page 1 of 2 1 2
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Naomi

[GLOBAL: userInfoPane.html]
Naomi
  • Corona Geek

  • 2,303 posts
  • Corona SDK

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
    $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
uid: 67217 topic_id: 29545 reply_id: 329545


[TOPIC: post.html]
#2

OwenYang

[GLOBAL: userInfoPane.html]
OwenYang
  • Contributor

  • 163 posts
  • Corona SDK

@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.
uid: 25057 topic_id: 29545 reply_id: 118572


[TOPIC: post.html]
#3

Naomi

[GLOBAL: userInfoPane.html]
Naomi
  • Corona Geek

  • 2,303 posts
  • Corona SDK

@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
uid: 67217 topic_id: 29545 reply_id: 118573


[TOPIC: post.html]
#4

Naomi

[GLOBAL: userInfoPane.html]
Naomi
  • Corona Geek

  • 2,303 posts
  • Corona SDK

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.)
uid: 67217 topic_id: 29545 reply_id: 118575


[TOPIC: post.html]
#5

OwenYang

[GLOBAL: userInfoPane.html]
OwenYang
  • Contributor

  • 163 posts
  • Corona SDK

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 :)

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 .:)
see it if this help
http://www.php.net/manual/en/function.json-decode.php
good luck
uid: 25057 topic_id: 29545 reply_id: 118574


[TOPIC: post.html]
#6

dev_wp7

[GLOBAL: userInfoPane.html]
dev_wp7
  • Enthusiast

  • 64 posts
  • Corona SDK

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


[TOPIC: post.html]
#7

OwenYang

[GLOBAL: userInfoPane.html]
OwenYang
  • Contributor

  • 163 posts
  • Corona SDK

@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.
uid: 25057 topic_id: 29545 reply_id: 118578


[TOPIC: post.html]
#8

Naomi

[GLOBAL: userInfoPane.html]
Naomi
  • Corona Geek

  • 2,303 posts
  • Corona SDK

@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
    $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
uid: 67217 topic_id: 29545 reply_id: 118580


[TOPIC: post.html]
#9

xnailbender

[GLOBAL: userInfoPane.html]
xnailbender
  • Contributor

  • 360 posts
  • Corona SDK

Hey there,

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

<?php
    $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
uid: 106779 topic_id: 29545 reply_id: 118582


[TOPIC: post.html]
#10

OwenYang

[GLOBAL: userInfoPane.html]
OwenYang
  • Contributor

  • 163 posts
  • Corona SDK

@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 :)
i think it works when use "GET" method
uid: 25057 topic_id: 29545 reply_id: 118583


[TOPIC: post.html]
#11

OwenYang

[GLOBAL: userInfoPane.html]
OwenYang
  • Contributor

  • 163 posts
  • Corona SDK

@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
<?php
    $deviceid = http_get_request_body()
    // then add device id into your database....
?>
uid: 25057 topic_id: 29545 reply_id: 118585


[TOPIC: post.html]
#12

Naomi

[GLOBAL: userInfoPane.html]
Naomi
  • Corona Geek

  • 2,303 posts
  • Corona SDK

@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
$userdata = http_get_request_body()
echo $userdata;
?>
-- terminal output:
RESPONSE:

-- attempt two
<?php
$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
uid: 67217 topic_id: 29545 reply_id: 118586


[TOPIC: post.html]
#13

OwenYang

[GLOBAL: userInfoPane.html]
OwenYang
  • Contributor

  • 163 posts
  • Corona SDK

@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
uid: 25057 topic_id: 29545 reply_id: 118587


[TOPIC: post.html]
#14

Naomi

[GLOBAL: userInfoPane.html]
Naomi
  • Corona Geek

  • 2,303 posts
  • Corona SDK

@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?
uid: 67217 topic_id: 29545 reply_id: 118589


[TOPIC: post.html]
#15

Naomi

[GLOBAL: userInfoPane.html]
Naomi
  • Corona Geek

  • 2,303 posts
  • Corona SDK

@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
uid: 67217 topic_id: 29545 reply_id: 118594


[TOPIC: post.html]
#16

xnailbender

[GLOBAL: userInfoPane.html]
xnailbender
  • Contributor

  • 360 posts
  • Corona SDK

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

uid: 106779 topic_id: 29545 reply_id: 118626


[TOPIC: post.html]
#17

budershank

[GLOBAL: userInfoPane.html]
budershank
  • Contributor

  • 358 posts
  • Corona SDK

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


[TOPIC: post.html]
#18

dev_wp7

[GLOBAL: userInfoPane.html]
dev_wp7
  • Enthusiast

  • 64 posts
  • Corona SDK

@OwenYang
Thnx 4 ur help ,i appreciate that :)
uid: 160777 topic_id: 29545 reply_id: 118713


[TOPIC: post.html]
#19

Naomi

[GLOBAL: userInfoPane.html]
Naomi
  • Corona Geek

  • 2,303 posts
  • Corona SDK

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
uid: 67217 topic_id: 29545 reply_id: 118772


[TOPIC: post.html]
#20

budershank

[GLOBAL: userInfoPane.html]
budershank
  • Contributor

  • 358 posts
  • Corona SDK

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.
uid: 147305 topic_id: 29545 reply_id: 118781


[TOPIC: post.html]
#21

Naomi

[GLOBAL: userInfoPane.html]
Naomi
  • Corona Geek

  • 2,303 posts
  • Corona SDK

@budershank, thank you for letting me know. So it works with python but not with php? And you are using HTTP POST method (not HTTPS) -- I know you said you are able to do this, but just to make tripple sure. I've been smashing my head on the wall way too long.

I'd so appreciate it if you can see any problem with the LUA/Corona portion of the code here? I know it's a lot of trouble, but if you could help me out, I'd so appreciate it. I just can't see what's wrong with it. Is there something wrong with my lua code? Would this lua code work with python? Is it that Corona implementation of HTTP POST method is incompatible with PHP? What could it be so wrong with what I'm doing here?

--LUA (corona side - plug & play except for the URL):
local URL = ... -- please include valid http url here
local function networkListener( event )
	if ( event.isError ) then
		print( "Network error!")
	else
		print ( "RESPONSE: " .. event.response )
	end
end

local function sendRequest(set_email, set_user)
    local postdata = {}
    local headers = {}
    headers["Content-Type"] = "application/json"
    headers["Accept-Language"] = "en-US"
    postdata.headers = headers
    local param = {}
    param.username = set_user
    param.email = set_email
    postdata.body = json.encode(param)
    network.request( URL, "POST", networkListener, postdata)
end

sendRequest("myemail@myweb.com", "mynickname")

-- PHP (just responding back with the data dump to see if anything is getting there)
<?php
    echo var_dump($_REQUEST);
?>

-- terminal output shows blank array
RESPONSE: array(0) {
}

Naomi
uid: 67217 topic_id: 29545 reply_id: 118787


[TOPIC: post.html]
#22

budershank

[GLOBAL: userInfoPane.html]
budershank
  • Contributor

  • 358 posts
  • Corona SDK

Well, looks like you are caught up where I got stuck(and got fed up). Though after playing with it I am thinking it seems like you are doing everything right so I am wondering if there is something on your webserver(presumably apache) or in your php.ini that is causing the post data to not be received. A quick google search shows that people do have that issue and there are some settings that can be tweaked.
uid: 147305 topic_id: 29545 reply_id: 118792


[TOPIC: post.html]
#23

Naomi

[GLOBAL: userInfoPane.html]
Naomi
  • Corona Geek

  • 2,303 posts
  • Corona SDK

@budershank, thank you for looking into this. It sounds like I have a lot more to sort out -- and honestly, tweaking server side setting is totally not my kind of thing. I'd rather have some super techie genius to get it done for me. But then, coding wasn't my kind of thing when I started a year ago, so maybe (just maybe) I could get lucky and somehow manage this... with lots more help from all over the web somehow. ** deep sigh **

Thanks again.

Naomi

Edit: But wait. The webserver is able to receive post data from the web form I created. I mean, I set up something like form.php and process.php on the same domain, and when I directly enter email address and username to the input box on form.php, process.php recognizes the post data and process it (and it even inserts these data to MySQL database I set up.) Is receiving post data from client app like Corona different from receiving post data from its own web form?? And do I need to change the server setting when I want php to receive post data from a client app? It seems odd to me, though.

Edit 2 And the thing is, the webserver responds perfectly fine when I use GET method (using it in both Corona app side and PHP side). So it seems too strange to think that HTTP POST method trouble (when used in conjunction with PHP) is a server side issue.
uid: 67217 topic_id: 29545 reply_id: 118793


[TOPIC: post.html]
#24

xnailbender

[GLOBAL: userInfoPane.html]
xnailbender
  • Contributor

  • 360 posts
  • Corona SDK

Hey there,

I've spent way to much time on this also. The POST method works as it is used during my IAP receipt verifications to my server and it handles the tables.

network.request(link,"POST",localListener,{body=postData})

Here params is replaced by {body=postData}, what I need are both and I'd like to include body in the params.

Nail
uid: 106779 topic_id: 29545 reply_id: 118797


[TOPIC: post.html]
#25

Naomi

[GLOBAL: userInfoPane.html]
Naomi
  • Corona Geek

  • 2,303 posts
  • Corona SDK

Nail @xnailbender, thank you for your post. I'd like to know how I may break you code snippet you posted down to basic mechanics so that I may use it to plug & play.

So... what do you have for postData? How is postData defined in your code that works? Once I can get the basics working, I know I can expand from there... but I can't see the content of the postData as it is, and therefore, I can't replicate the process that worked for you. Could you possibly show me what postData holds inside, please?

Naomi
uid: 67217 topic_id: 29545 reply_id: 118800



[topic_controls]
Page 1 of 2 1 2
 
[/topic_controls]