[java] Getting a java applet to call php scripts?

Started by
3 comments, last by CodeMunkie 13 years, 11 months ago
Hi Guys, I used an Applet to make a racing car game. The game works pretty well. I want to now integrate it with Facebook and will need to call some of there API's. I am slightly familiar with PHP. Facebook supports 2 languages for there API officially. PHP and &#106avascript. There is a java API but its not official and I dnt know much about it. So to the question.(Guess its more of a design problem) Can applets work effectively to pass and receive data from php scripts. The php scripts will then interface with Facebook servers and my database. If there were 50 users playing the applet and calling these php scripts on the server should it be able to handle it? Would the design that I am thinking about seem feasible? Its my first attempt at web programming so apologies if I am way off. Thanks Set
Advertisement
I did something like this a long time ago, but it was pretty primitive. Basically the applet would attempt to open a local web-page with all of the data being passed in the url, and the php script would return a "web page" which contained data in an ascii format. There might be a cleaner way to do it, but that works. Here's the java code. Sorry about the formatting (or lack thereof).

public String connect(String page, String vars[], String values[]){  String output="" ; try{  URL                 url;    URLConnection urlConn;    DataOutputStream    printout;    DataInputStream     input;    // URL of CGI-Bin script.    url = new URL (getCodeBase().toString() + page);        // URL connection channel.    urlConn = url.openConnection();    // Let the run-time system (RTS) know that we want input.    urlConn.setDoInput (true);    // Let the RTS know that we want to do output.    urlConn.setDoOutput (true);    // No caching, we want the real thing.    urlConn.setUseCaches (false);    // Specify the content type.    urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");    // Send POST output.    printout = new DataOutputStream (urlConn.getOutputStream ());    String content = "" ;    for(int k=0;k<vars.length;k++){    	content+=URLEncoder.encode (vars[k])+"="+URLEncoder.encode (values[k]) ;    	content+="&" ;    }	    printout.writeBytes (content);    printout.flush ();    printout.close ();    // Get response data.    input = new DataInputStream (urlConn.getInputStream ());    String str;    while (null != ((str = input.readLine()))){output+=str+"\n";}    input.close ();          System.out.println("score sent successfully!") ;}catch(Exception e){	System.out.println("Connection failed due to error");		System.out.println(e) ;  }return output ; }
Hi Alrecenk,

Is there not some standard feature/api of java for handling interfacing with php scripts? Since it seems you had to hack away around this. If people dnt use php what are people using instead?

The URLEncoder is used for simple communication with GET methods.

To use POST, PUT or other functions you could look at the URLConnection class.
Quote:Is there not some standard feature/api of java for handling interfacing with php scripts? Since it seems you had to hack away around this. If people dnt use php what are people using instead?


"Interfacing with PHP scripts" is a matter of sending HTTP requests to a web server. What you could do is create a submit_score.php script on your web server. Contruct a web request as Alrecenk has demonstrated and call your submit_score.php with whatever parameters you like. submit_score.php will update your database and can additionally do whatever "Facebook" stuff you want it to do by using the PHP API.

Quote:If there were 50 users playing the applet and calling these php scripts on the server should it be able to handle it? Would the design that I am thinking about seem feasible?


It depends on how many requests they are sending to the web server and the capacity of the web server. Remember, the applet is running on the client's computer, so once they have downloaded it, they are not generating any load on the server. If you are just sending high score info, then the only time you would be generating web traffic would be when they actually submit a score, which I assume would be at "Game Over" which should not be happening that often per client.
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.

This topic is closed to new replies.

Advertisement