- Home /
Get echo output from PHP script which is on an FTP server with C#
Yeah I know, quite the title. Allow me to explain,
For my school exams I am tasked with making a game which has to be playable in a browser, to do this, I'm using unity's webplayer.
The game needs to keep track of highscores and such so I made a MySQL database which holds the scores.
To connect to the database and read out the scores and other such data, I have written a PHP script. The PHP script is on an FTP server.
The PHP script echo's the data it reads form the database, so if you'd run it in your browser, u'd get some text back like: "username;score".
Now here comes the problem,
I used to use a C# script to read the output form the PHP file(the username + score). I did this using unity's WWW class, however, sicne the PHP file has recently been placed onto the FTP server, a WWW request wont work anymore.
I've managed to get as far as connecting to the FTP server and reading the actual PHP file (the actual PHP code), but that's of course useless to me! I need the html output it would give.
Here's the code for all my scripts:
The PHP code:
$dorpNaam;
$dorpX;
$dorpY;
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if (!$db_handle) {
die('Could not connect: ' . mysql_error());
}
$dorpNaam;
if (isset($_GET['dorpNaam'])){
$sql = "SELECT dorpNaam,dorpOmtrek,dorpX,dorpY FROM dorpen_en_steden";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) // loop to give you the data in an associative array so you can use it however.
{
echo $row['dorpNaam']."-".$row['dorpOmtrek']."-".$row['dorpX']."-".$row['dorpY'].';';
}
}else {
$sql = "SELECT dorpNaam FROM dorpen_en_steden";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) // loop to give you the data in an associative array so you can use it however.
{
echo $row['dorpNaam'].';';
}
}
mysql_close($db_handle);
?>
The C# code:
void ftp() {
string serverPath = "ftp://ndc38.colo.bit.nl/map/ReadCities.php";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath);
request.KeepAlive = true;
request.UsePassive = true;
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential("helidrop", "5c1tLuae");
// Read the file from the server & write to destination
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
using (StreamWriter destination = new StreamWriter("hoi.txt"))
{
destination.Write(reader.ReadToEnd());
destination.Flush();
}
}
If any one has any advice on what to do, either how to get the echo form the PHP file or an alternative maybe? (as long as it doesn't take to much time to implement). I'd be incredibly grateful :)
PS: I hope the post is kinda clear :P
from ftp cO??? Can you FTP Server Proccess PHP files? Normaly you need something like Apache running on your Server.
I don't think it can, I'm not really in charge of the serve, Everything I want to change, I have to request from the server maintenance guy. But are you saying that if I get apache running on the server, I could run the PHP script? How would I do that?
PS: I DO have WA$$anonymous$$P installed(and apache) on my own PC. Thanks for your awnser.
Answer by Dave-Carlile · Apr 04, 2013 at 01:29 PM
An FTP server doesn't process PHP, or any other language - it simply serves up files. You need a web server in order to process/execute the PHP scripts. Setting up a web server is out of the scope of Unity Answers. You should have no trouble finding assistance using Google. But, I would hope your server maintenance guy would already have a web server set up, or would be able to do it for you.
Thanks for your reply it cleared things up a little bit. I told the the server maintenance guy that i needed a webserver to run the php files, and he told me that the ftp server already is a webserver , running php5, and that I just need to put the website on it.
I'm still a bit confused as to how to do this though, because I simply have the php files, no webesite build around it or anything.
Ins$$anonymous$$d of ftp://ndc38.colo.bit.nl/map/ReadCities.php
, try http://ndc38.colo.bit.nl/map/ReadCities.php
And use Http response ins$$anonymous$$d. This should go through the http protocol rather than ftp, and the web server will execute the PHP to produce the web page. This is assu$$anonymous$$g the web server is configured correctly.
Also, you might look into use Unity's WWW class rather than the .NET classes.
Thanks dave I've got it all working now, I got some extra help from the server guy too :P
I was already actually using the WWW class but I got so confused witht the whole ftp thing, I thought maybe i needed a diferent way of connecting to it :P
Thanks everyone for the help, this question can be closed :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Download multiple files from server 2 Answers
Is it possible to create GameObjects dinamically from a server? 1 Answer