I have no experience in php so i don’t know how I can get a data from a php script to my app. Can anyone give me a sample php script? just a simple one. And what should i use to grab that data to my app?
Thanks in advance. [import]uid: 189861 topic_id: 34830 reply_id: 334830[/import]
I think you first have to decide what the php script is supposed to do, then how you want to get the data. What I mean is, should the script pull data from a database or what exactly do you want to send to the “client” (mobile device).
Next, you need to decide how you want to receive the data (XML or JSON). My personal preference is JSON, but I’ve had the need to do XML from time to time as well. Once you’ve done that, and you build the script (API), the next step is to retrieve the data, and this is where the network.request command comes in.
I know this is rather general, but thought you might like a high level overview of the process which may help you better look for an answer to what you really want to do.
Good luck! [import]uid: 64538 topic_id: 34830 reply_id: 138470[/import]
This might help you too:
http://omnigeek.robmiracle.com/2012/04/15/using-corona-sdk-with-rest-api-services/
Also, it will help you greatly if you learn some php (or python, which I hear about a lot.)
Good luck.
Naomi
[import]uid: 67217 topic_id: 34830 reply_id: 138481[/import]
Thanks Naomi. I was going to point him there myself
[import]uid: 199310 topic_id: 34830 reply_id: 138537[/import]
Thanks @naomi for the link, I’ll go and read it. [import]uid: 189861 topic_id: 34830 reply_id: 138742[/import]
@gbailey not necessarily a database, I just want to get a string value from the php side. [import]uid: 189861 topic_id: 34830 reply_id: 138744[/import]
and also I’m using xampp for my server. [import]uid: 189861 topic_id: 34830 reply_id: 138747[/import]
@www.
FWIW, I’m a newb and don’t know much about php,
Here is a simple way, but not very secure, of getting a string from a php script.
I’m using GET here where you can simply add the params to the end of your URL
Just point you network.request at this PHP scripts URL.
Save this as downloadString.php
<?php <br>
$String = "Place your string here";
$PlayerID = 345345;
$Error = "Server Error";
$login = $\_GET['login'];
$publishString = json\_encode
(
array(
'String' =\> $String,
)
);
if ($PlayerID == $login) echo $publishString;
else echo $Error;
?\>
Now here’s you lua side of it.
local json = require("json")
local PlayerID = 345345
local DownloadedString = 0
local function stringDownload(params)
local downloadListener = params.downloadListener or function(event) end
local DownloadServerLink = params.DownloadServerLink
DownloadLink = DownloadServerLink
local function downloadListener(event)
local response = event.response --this is the json file returned from the echo php call
print("downloadListener(event) has bee executed")
print("event.response == ", response)
local decodedStats = json.decode(response)
if type(decodedStats) == "table" then
DownloadedString = decodedStats.String
print("DownloadedString = ", DownloadedString)
else
end
end
network.request(DownloadLink,"POST",downloadListener )
print("stringDownload() has been executed, we hope")
end
local function prepStringDownload()
stringlDownload
{
downloadListener = downloadListener,
DownloadServerLink = "http://yoursite.com/downloadString.php?login="..PlayerID
}
end
local function startStringDownload(event)
if event.phase == "ended" then
prepStringDownload()
end
return true
end
It’s late…
I know it’s not elegant and I didn’t test this, there may be typos, errors and local may be missing on some varialbles, but it should be a good start at what you are after I think.
Hope this helps,
Nail [import]uid: 106779 topic_id: 34830 reply_id: 138777[/import]
Thanks Nail. I’ll go and try your code. [import]uid: 189861 topic_id: 34830 reply_id: 138928[/import]
I think you first have to decide what the php script is supposed to do, then how you want to get the data. What I mean is, should the script pull data from a database or what exactly do you want to send to the “client” (mobile device).
Next, you need to decide how you want to receive the data (XML or JSON). My personal preference is JSON, but I’ve had the need to do XML from time to time as well. Once you’ve done that, and you build the script (API), the next step is to retrieve the data, and this is where the network.request command comes in.
I know this is rather general, but thought you might like a high level overview of the process which may help you better look for an answer to what you really want to do.
Good luck! [import]uid: 64538 topic_id: 34830 reply_id: 138470[/import]
This might help you too:
http://omnigeek.robmiracle.com/2012/04/15/using-corona-sdk-with-rest-api-services/
Also, it will help you greatly if you learn some php (or python, which I hear about a lot.)
Good luck.
Naomi
[import]uid: 67217 topic_id: 34830 reply_id: 138481[/import]
Thanks Naomi. I was going to point him there myself
[import]uid: 199310 topic_id: 34830 reply_id: 138537[/import]
Thanks @naomi for the link, I’ll go and read it. [import]uid: 189861 topic_id: 34830 reply_id: 138742[/import]
@gbailey not necessarily a database, I just want to get a string value from the php side. [import]uid: 189861 topic_id: 34830 reply_id: 138744[/import]
and also I’m using xampp for my server. [import]uid: 189861 topic_id: 34830 reply_id: 138747[/import]
@www.
FWIW, I’m a newb and don’t know much about php,
Here is a simple way, but not very secure, of getting a string from a php script.
I’m using GET here where you can simply add the params to the end of your URL
Just point you network.request at this PHP scripts URL.
Save this as downloadString.php
<?php <br>
$String = "Place your string here";
$PlayerID = 345345;
$Error = "Server Error";
$login = $\_GET['login'];
$publishString = json\_encode
(
array(
'String' =\> $String,
)
);
if ($PlayerID == $login) echo $publishString;
else echo $Error;
?\>
Now here’s you lua side of it.
local json = require("json")
local PlayerID = 345345
local DownloadedString = 0
local function stringDownload(params)
local downloadListener = params.downloadListener or function(event) end
local DownloadServerLink = params.DownloadServerLink
DownloadLink = DownloadServerLink
local function downloadListener(event)
local response = event.response --this is the json file returned from the echo php call
print("downloadListener(event) has bee executed")
print("event.response == ", response)
local decodedStats = json.decode(response)
if type(decodedStats) == "table" then
DownloadedString = decodedStats.String
print("DownloadedString = ", DownloadedString)
else
end
end
network.request(DownloadLink,"POST",downloadListener )
print("stringDownload() has been executed, we hope")
end
local function prepStringDownload()
stringlDownload
{
downloadListener = downloadListener,
DownloadServerLink = "http://yoursite.com/downloadString.php?login="..PlayerID
}
end
local function startStringDownload(event)
if event.phase == "ended" then
prepStringDownload()
end
return true
end
It’s late…
I know it’s not elegant and I didn’t test this, there may be typos, errors and local may be missing on some varialbles, but it should be a good start at what you are after I think.
Hope this helps,
Nail [import]uid: 106779 topic_id: 34830 reply_id: 138777[/import]
Thanks Nail. I’ll go and try your code. [import]uid: 189861 topic_id: 34830 reply_id: 138928[/import]