|
Author |
Topic: loading variables from php (Read 615 times) |
|
kman Guest
|
loading variables from php
« on: Jul 28th, 2003, 2:49pm » |
|
Hi, i'm stuck in trying to load variables from a php script. I'm trying to port some flash pieces that use variables to generate grafic patterns. This is how i try to do it now : // code starts here String words = loadStrings("http://www.nmn.be/food/php/tel_letters.php"); String letters[] = splitStrings(words, '&'); println("!!!!there are " + letters.length + " lines"); for (int i= 0; i < letters.length; i++) { println(letters[i]); } // code ends here This is based on an example with a local text file, that works. I get an error : "Local variable "letters" may have not been initialized before use [JLS 14.4] Can someone clear this out for me. I would very much like to work with in interaction with serverside scripts to exchange data & variables. Thanks a lot K
|
|
|
|
toxi
|
Re: loading variables from php
« Reply #1 on: Jul 28th, 2003, 3:38pm » |
|
your problem is loadStrings() doesn't just return a string, but a string array with one line per item. i assume your php script is just returning one line of "&" separated words. so you code would look like this: Code:String[] lines = loadStrings("http://www.nmn.be/food/php/tel_letters.php"); if (lines.length>0) { String[] words = splitStrings(lines[0], '&'); println("!!!!there are " + words.length + " words"); for (int i= 0; i < words.length; i++) { println(words[i]); } } else println("no words found"); |
|
|
http://toxi.co.uk/
|
|
|
kman Guest
|
Re: loading variables from php
« Reply #2 on: Jul 28th, 2003, 4:06pm » |
|
thanks for the info. You are right, my php returns 1 line line of "&" separated words. And i see now that loadStrings returns an array (should have looked at the refference better). But i tried your code suggestion and i get nothing for response. Not a single println, nor error. Can you help me any further? K
|
|
|
|
toxi
|
Re: loading variables from php
« Reply #3 on: Jul 28th, 2003, 4:36pm » |
|
don't know what's wrong, it works fine on my machine... i'm getting the output below: !!!!there are 29 words total=1059 a=8 b=2 c=2 d=4 e=12 etc. in my experience you sometimes have to save and/or restart processing in order to see any code updates (especially if you're not using the normal setup() / loop() structure). that's all because we're still in alpha phase and the parser still has bugs.
|
http://toxi.co.uk/
|
|
|
kman Guest
|
Re: loading variables from php
« Reply #4 on: Jul 28th, 2003, 4:51pm » |
|
thanks for the testing. maybe my problems come from being behind a proxy from my isp. any tips on that? i'll test some more. if it works for you, i should get it to work also. thanks again K
|
|
|
|
mKoser
|
Re: loading variables from php
« Reply #5 on: Jul 28th, 2003, 5:30pm » |
|
I thought I'd just give it a go to see if i could Toxis example working - but no luck kman, if you get it working, would it be too much to ask to make a post and explain what you did? Mikkel
|
mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
|
|
|
kman Guest
|
Re: loading variables from php
« Reply #6 on: Jul 29th, 2003, 4:58pm » |
|
Hi, I still don't get it to work. Anyone a suggestion as to what can cause this not to work. I also tried a different way with this code : // code start void setup() { loadLetters(); } void loadLetters() { try { InputStream input = loadStream("http://www.nmn.be/food/php/tel_letters.php"); InputStreamReader isr = new InputStreamReader(input); BufferedReader reader = new BufferedReader(isr); String line; while ((line = reader.readLine()) != null) { println("next line is: " + line); } } catch (IOException e) {println("warning, i/o error:"+e);} } // code end Here i get this error : "warning, i/o error:java.net.ConnectException: Connection timed out: connect" What could cause me not getting on the internet from within the application. Any thoughts would be appreciated. Thanks
|
|
|
|
ASCII33
|
Re: loading variables from php
« Reply #7 on: Jul 30th, 2003, 7:35pm » |
|
I just tried the Toxi code myself, and it works just fine on my OsX 10.3, Processing 0056 setup. Looks promising ! :j
|
|
|
|
mKoser
|
Re: loading variables from php
« Reply #8 on: Jul 31st, 2003, 12:22pm » |
|
ok, now I got it working aswell allthough I have only tested it on an internal server (only visible through our company network) ... I'll give it a go later on my own server! i've included the php code snippet! Code: // PHP communication // mikkel crone koser // based on kman and toxi code // exchange "http://myservername/respond.php" // with the location of your php-file. String[] reply; void setup(){ } void mousePressed(){ reply = loadStrings("http://myservername/respond.php"); if (reply.length>0) { String[] words = splitStrings(reply[0], '&'); println("!!!!there are " + words.length + " words"); for (int i= 0; i < words.length; i++) { println(words[i]); } } else println("no words found"); } /* tested with the following PHP code: <?php echo "proce55ing&"; echo "webserver&"; echo "communication"; ?> */ |
| ...added 01 august i just tried it on my normal webserver aswell, no problem (this time on my home ADSL) http://www.beyondthree.com/_dev/respond.php
|
« Last Edit: Aug 1st, 2003, 11:51am by mKoser » |
|
mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
|
|
|
toxi
|
Re: loading variables from php
« Reply #9 on: Jul 31st, 2003, 1:18pm » |
|
i think from within the PDE (and at least v0056) this feature should work with any URL (and local files). once you export the applet it will only work with URLs in the same domain... i'm only saying this as i got security exceptions when my applet tried to access kman's server (www.nmn.be) more info can be found here: http://java.sun.com/docs/books/tutorial/applet/overview/security.html
|
http://toxi.co.uk/
|
|
|
kman Guest
|
Re: loading variables from php
« Reply #10 on: Jul 31st, 2003, 1:30pm » |
|
Hi, mKoser, i tried your example to and still no luck. I try all examples from within the PDE (v0056) on win XP home. I must add that i use a cable internet connection which goes through a proxy server of my isp. Could this be a reason? Toxi, i was aware of those security things. But it doesn't work for me as an applet from the same domain either. Any thoughts on solving this are most welcome, because i can't think of any way to get this to work. thanks
|
|
|
|
pollux
|
Re: loading variables from php
« Reply #11 on: Sep 18th, 2003, 11:43pm » |
|
i have an applet that parses an external url via a php file. this is the method for parsing the php file output. Code://// getting and parsing data //// php otput is something like ( <38>@1;<38>@5;<38>@19;. ) void getData() { String fileName; String data = "<38>@1;<38>@5;<38>@19;."; //// data download //// if (!authoring) { String codeBase = getCodeBase().toString(); fileName = codeBase+"ptt.php?busNum=1&stop="+fermataS+"&bus1="+linee[0]; try { String d; //// queries the ptt.php file for the data URL datastream = new URL(getCodeBase()+fileName); BufferedReader in = new BufferedReader(new InputStreamReader(datastream.openStream())); while ((d = in.readLine()) != null) { data = d; } in.close(); } catch (Exception e) { errorString = String.valueOf(e); println( errorString ); } } //// parses the received data, and stores it into the parsedData array int EOF = data.indexOf("."); int j=0; int i=0; boolean b; while (j < EOF) { b = true; if (j < EOF) parsedData[i][0] = new parseData(b,data,j).nextField(); j = parseData.getPos(); b = false; if (j < EOF) parsedData[i][1] = new parseData(b,data,j).nextField(); j = parseData.getPos(); i++; } parsed = true; } //// divides the data chunks into [line][time] pairs class parseData { String FIELD_START = "<"; String FIELD_END = ">"; String str; int back = 0; static int pos; parseData(boolean b, String s, int i) { str = s; pos = i; if (!b) { FIELD_START = "@"; FIELD_END = ";"; } } int nextField() { int tmp = str.indexOf(FIELD_END, pos); String nf = str.substring((str.indexOf(FIELD_START, pos))+1, tmp); pos = tmp+1; back = Integer.parseInt(nf); return back; } static int getPos() { return pos; } } |
| hope it helps.
|
pollux | www.frwrd.net
|
|
|
|