FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Integration
(Moderators: fry, REAS)
   Processing <-> PHP <-> MySQL
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Processing <-> PHP <-> MySQL  (Read 10957 times)
marcello

WWW Email
Processing <-> PHP <-> MySQL
« on: Jan 28th, 2005, 11:38am »

Hi,
is it possible for an applet built with processing to communicate (in both ways) with a PHP script that manages a database MySQL?
 
I'm using a free web space that supports PHP and MySQL and i would like to learn how an applet built with processing could load and save text files from/to this web space.
 
does anybody know some code that i could study.
 
Thanks in advance,
Marcello
« Last Edit: Jan 28th, 2005, 11:39am by marcello »  
eskimoblood

222550793222550793 WWW
Re: Processing <-> PHP <-> MySQL
« Reply #1 on: Jan 28th, 2005, 1:11pm »

Hi I wrote a mapviewer for the umnmapserver. For the communication between the mapserver cgi and my applet I use a invisible frame with a formular. The formular was filled out and send by the apllet. I use the netscape.javascript Object to do this. The response from the cgi was the a site with a javascript which was start by the onload event. This script send some variables to a function in my applet.  
Here are some piece of my code
 
Fill out the formular:
Code:

JSObject parent, doc, form, frame, form_mode, form_imgbox, form_imgxy, form_zoom, form_layer;
 
// get the javascript objects
    parent=(JSObject) jsWin.getMember("parent");
    frame=(JSObject) parent.getMember("formframe");
    doc=(JSObject) frame.getMember("document");
    form=(JSObject) doc.getMember("formular");
 
    form_mode=(JSObject) form.getMember("mode");
    form_imgbox=(JSObject) form.getMember("imgbox");
    form_imgxy=(JSObject) form.getMember("imgxy");
    form_zoom=(JSObject) form.getMember("zoom");
    form_layer=(JSObject) form.getMember("layers");
 
//set the values of the formular
    theJS.form.setMember("target","queryframe");
    theJS.form_mode.setMember("value","query");
    theJS.form_imgxy.setMember("value",p);
    theJS.form_layer.setMember("value","kleks_f kleks_p kleks_l");
    theJS.form_zoom.setMember("name","");
    theJS.form_imgbox.setMember("name","");

 
Javascript to call a function in my applet
Code:

   function sendToApplet() {
    var Id = "[ID]";
    var Typ = "[TYP]";
    var Name = "[NAME]";
    var Schutz = "[SCHUTZ]";
    var Zustand = "[ZUSTAND]";
    var Entstehung = "[ENTSTEHUNG]";
    var Untergang = "[UNTERGANG]";
    var Text = "[TEXT]";
    var HTML = "[HTML]";
    try{
// check if the applet is loaded cause otherwise the script will crash      if(parent.viewframe.document.viewer.getQueryData(Id, Typ, Name, Schutz, Zustand, Entstehung, Untergang, Text,HTML)) throw "on";
    }
    catch (e){
     if(e!="on") setTimeout("sendToApplet();",500);
    }
   }
 
fjen

WWW
Re: Processing <-> PHP <-> MySQL
« Reply #2 on: Jan 28th, 2005, 4:38pm »

hi marcello,
 
i have something here which might help you. i made this to read-write image data from processing to the server. check out the sendImage() function in the pde which uses POST to send the data to the script thru a http-socket.
 
/F
« Last Edit: Jan 28th, 2005, 4:38pm by fjen »  
marcello

WWW Email
Re: Processing <-> PHP <-> MySQL
« Reply #3 on: Jan 28th, 2005, 5:50pm »

Thank you very mutch guys,
 
I'll start to study your codes. If I'll have some problem I'll tell you here
 
Best Regards,
Marcello
 
cello

marcello3d WWW
Re: Processing <-> PHP <-> MySQL
« Reply #4 on: Jan 28th, 2005, 6:18pm »

on Jan 28th, 2005, 4:38pm, fjen wrote:
hi marcello,
 
i have something here which might help you. i made this to read-write image data from processing to the server. check out the sendImage() function in the pde which uses POST to send the data to the script thru a http-socket.
 
/F

 
Is there a particular reason you send the image as ascii and not binary  (Or even encode it as a jpg/png on the client-side to save bandwidth/speed up the transfer.)  I only mention this because I've do it on 2draw for people submitting their drawings.
 
But in any case for Marcello's purposes with text files, fjen's approach is better than using javascript.  More compatible and more direct.  
 
To summarize what fjen is doing, essentially you want to open your own HTTP connection and send in the data that way (if you're sending small data, it's probably easiest to use GET and include all the data in the URL, that way you don't need to build a custom http request), then for retrieving data, Processing already has a few functions for getting data from a URL.
 
Marcello
 
fjen

WWW
Re: Processing <-> PHP <-> MySQL
« Reply #5 on: Jan 28th, 2005, 9:30pm »

yeah, the code is a little dated. sorry i wasn't aware of that. later i did just what you do on 2draw.
 
anyway, sending images was not the question and you explained well what i wanted to show ...
 
applet:
- open socket
- send data
- wait for answer (? "ok")
 
php:
- open file (or mysql-connection)
- write to file (...)
- close file
- echo "ok"
 
so the important part would be:
Code:

String sendData (String data)
 {
  Socket echoSocket = null;
  PrintWriter out = null;
  BufferedReader in = null;
 
  String server = "tente-argent.local"; // some.server.com // needs to be the same server the applet is running on //
  //status("Server ("+server+")");
  String script = "/PHP/JPEGStreamPHP.php"; // /path/to/script //
 
  try {
   echoSocket = new Socket( server, 80 ); // http connection to server //
   out = new PrintWriter(echoSocket.getOutputStream(), true);
   in  = new BufferedReader(new InputStreamReader( echoSocket.getInputStream()));
 
  } catch (UnknownHostException e) {
   println("Don't know about host: " + server);
 
  } catch (IOException e) {
   println("Couldn't get I/O for the connection to: " + server);
  }
 
  // prepare for talkin ... //
  String server_response;
  String poststring = "data="+URLEncoder.encode(data);
 
  // string to send //
  String send_string = "\r\nPOST " + script + " HTTP/1.1\r\n" +
   "Host: " + server + "\r\n" +
   "Content-type: application/x-www-form-urlencoded\r\n" +
   "Content-length: " + (new Integer(poststring.length()).toString()) + "\r\n" +
   "Connection: close\r\n\r\n" +
  poststring + "\r\n\r\n";
 
  // send //
  out.println(send_string);
   
  // receive //
  String filename = "-1";
  try {
   while ((server_response = in.readLine()) != null)  
   {
    //println( server + ": " + server_response );
    if (server_response.indexOf("ok") != -1) {
     filename = server_response;
    }
   }
 
  out.close();
  in.close();
  echoSocket.close();
 
  } catch (Exception e) {
   println("Error reading server response.");
   e.printStackTrace();
  }
   
  return filename;
 }

 
/F
 
Euskadi


Re: Processing <-> PHP <-> MySQL
« Reply #6 on: Jan 29th, 2005, 4:15am »

Yep, there's a few ways to do this.
 
I'm working on a simulator that stores some data about the simulation for retrieval later.
 
1) Create a php page to receive requests and respond with a very basic page with the data you want.
 
Here's a sample of a function that hits a php page and gets some info based on the session ID. (somebody help me, how do I make the "code" code?
 
String captureID(){
 String getID = "http://" + strURL + "/simulate/getcaptureid.php?sid=" + sessID;
 String c = "-1";
 try{
   String html[] = loadStrings(getID);
   for(int i =0; i < html.length; i++){
   if(html[i].length() !=0){
   c = html[i];
   }
   }
 }  catch (Exception e) {
   c = "-1";
 }
 return c;
}
 
2) Create something to send info to your database. You can use the same method as in 1) above. Or, like the previous message you can use the post method. Using "post" is nice because you can send a LOT of info that way. If you use the send method, putting the info in your url you are limited to 2000 characters or something.
 
This is post, large amounts of data:
 
void postNewItem (String S) {
 try {
   URL      url;
   URLConnection urlConn;
   DataOutputStream   dos;
   DataInputStream    dis;
 
   url = new URL("http://" + strURL + "/simulate/statscapture.php");
   urlConn = url.openConnection();
   urlConn.setDoInput(true);
   urlConn.setDoOutput(true);
   urlConn.setUseCaches(false);
   urlConn.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");
 
   dos = new DataOutputStream (urlConn.getOutputStream());
 
   String message=S;
   dos.writeBytes(message);
   dos.flush();
   dos.close();
 
   // the server responds by saying
   // "SUCCESS" or "FAILURE"
 
   //todo next 3 undo
   dis = new DataInputStream(urlConn.getInputStream());
   String s =""; // = dis.readLine();
   dis.close();
 
   if (s.equals("SUCCESS")) {
     ;   } else {
     ;
   }
 
 } // end of "try"
 
 catch (MalformedURLException mue) {
   ;  
 }
 catch (IOException ioe) {
   ;  
 }
 
}  // end of postNewItem() method
 
 
What I left out on this was the php page to receive the communication from either the post or send method and write to or query mysql... but there is lots out there on that topic.
 
 
fjen

WWW
Re: Processing <-> PHP <-> MySQL
« Reply #7 on: Jan 29th, 2005, 5:41am »

how to make the "code" code (like that):
 
without spaces after "[ " and before " ]":
 
[ code ] ... [ /code ]
[ link=URL ] ... [ /link ]
where URL is something like http://www...
[ b ] ... [ /b ]
[ i ] ... [ /i ]
[ quote author=... link=yabbURL ] ... [ /quote ]
where yabbURL is everything after the ...
 
i think there are some more ...
/F
 
marcello

WWW Email
Re: Processing <-> PHP <-> MySQL
« Reply #8 on: Mar 6th, 2005, 6:54pm »

Thank you all for your answers. I'm learning a lot from you. Unfortunately i was very busy lately with my job so i hadn'd so much time to test all your suggestions.
 
I had success in implementing the Euskadi's method n.1. I saw a similar thing in lifevslife's source code.
 
Now i would like to learn the fjen's method/Euskadi's method n.2. This method is a little bit difficult than previous. In the first i simply used the p5 loadStrings function. With the second i'll have to learn a bit of official java. I'll have also to study the fjen's php code.
 
 
Thank you all again,
Marcello
« Last Edit: Mar 6th, 2005, 7:42pm by marcello »  
seltar


Re: Processing <-> PHP <-> MySQL
« Reply #9 on: Mar 7th, 2005, 1:08am »

Dynamic function that POSTS multiple data to a dynamic php-script, and writes info to textfile
 
http://wliia.org/projects/postit/
 
think source code speaks for itself, and if it doesn't just drop me a line..
 
-seltar
 
marcello

WWW Email
Re: Processing <-> PHP <-> MySQL
« Reply #10 on: Mar 7th, 2005, 11:03am »

Thank you seltar,
this is also a very useful example.
 
Thanks to you all, i think that a lot of people will be now able to save and load data in a web account in a very simple way.
 
Best Regards,
Marcello
 
klaus_topmueller


Re: Processing <-> PHP <-> MySQL
« Reply #11 on: Mar 7th, 2005, 3:54pm »

Thanks seltar,
 
but does anybody know how to upload the displayed image as binary-jpeg to the server?
 
florian sends it as pixel-string -
Query-String: pixels=ffffffcccccc999999ff00aac7a49c ..."  via http://www.florianjenett.de/p55/socket_image/socket_image.pde
 
this generates too much ascii-data for me - my picture-size is (800x600px).
 
-klaus
 
 
marcello

WWW Email
Re: Processing <-> PHP <-> MySQL
« Reply #12 on: Apr 6th, 2005, 4:58pm »

Hi all,  
i've implemented the function that Euskadi ans seltar posted above time ago. With that function i was able to send text files to a php script wich receives these files by means of the command $_POST.
 
Now i'm having some trouble with different versions of java.
 
I have a PC with Windows ME installed. I'm also using IE 5.5.
 
1. if i use the default Microsoft Java Virtual Machine the above function works very well. I tried that function in two ways having success in both cases: before i used the server apache + php installed on my pc, and then i tried the same thing directly on a web space with php. All works well.
2. The problem arises when i use the sun jvm 1.4.2. In this case i don't get any result: it seems that the php script doesn't receive any data. This happens either with the server on my pc and the server on a web space. I have the same problem when i run the above function inside the processing developement environment.  
 
Now i'm asking what the problem could be. I looked at the java 1.4 documentation in order to see if someone of the methods in that function are deprecated, but it isn't so. Maybe the problem is in Windows ME or in the browser, but these are just conjectures.
 
Has anybody some idea where the problem could be
 
Thanks in advance,
Marcello
 
P.S. just to add the link to a related topic:
http://processing.org/discourse/yabb/board_Integrate_action_dis_play_num_1090838754.html
« Last Edit: Apr 13th, 2005, 3:39pm by marcello »  
marcello

WWW Email
Re: Processing <-> PHP <-> MySQL
« Reply #13 on: Apr 13th, 2005, 7:44pm »

OK! I found the problem.
 
I wrote the above function without the following portion of code:
 
Code:

   DataInputStream    dis;  
   ...
   ...
   ...
   dis = new DataInputStream(urlConn.getInputStream());
   String s = dis.readLine();  
   dis.close();  

 
since i believed that it was not necessary.
 
This was the error. In another previous thread (i just added the link in my previous post) i found the following link that Euskadi posted:
 
http://www.devdaily.com/java/edu/pj/pj010023/index.shtml
 
where it is clearly explained that:
 
Quote:
Notice that it's also necessary (and desirable) to read a response from the web server.

 
I added that portion of code and now the function works well also with sun jvm 1.4.2
 
Marcello
 
Pages: 1 

« Previous topic | Next topic »