only1wizard
asked on
how to call php script in java
hello - i would like to know how to call a php script from java applet.
outcome - passing java parameters to php
can you provide a working example?
thanks in advance for your help!
outcome - passing java parameters to php
can you provide a working example?
thanks in advance for your help!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No, I only have the PHP side. Leave the question open for a while and maybe a Java programmer will come along with the other half of the solution!
ASKER
ok will do thank u
ASKER
thanks for your example of the web service!
i have solved this problem the java works fine it was the php. solution i resolved the problem by focusing on getting the php script working alone with out the java once it ran on its own i recompiled java and ran the file and it works.
thanks in advance for your help!
i have solved this problem the java works fine it was the php. solution i resolved the problem by focusing on getting the php script working alone with out the java once it ran on its own i recompiled java and ran the file and it works.
thanks in advance for your help!
Take a look at the java.net.URL and java.net.HttpURLConnection classes. You should be able to use those to call the PHP web service and pass whatever parameters are required, then read the response as an InputStream in your applet. If the PHP service is not located on the same web server, you'll have to sign your applet or you won't be able to communicate with it.
Sample use of HttpURLConnection:
I left out the exception handling, but you'll need to account for those as well. Mostly subclasses of IOException, but there are some others as well (MalformedURLException for one). Hope that helps!
Sample use of HttpURLConnection:
public Object queryService(Object param1, Object param2) {
String encParam1 = URLEncoder.encode(param1.toString(), "UTF-8");
String encParam2 = URLEncoder.encode(param2.toString(), "UTF-8");
URL serviceURL = new URL(String.format("http://www.yourserver.com/webService.php?param1=%s¶m2=%s", encParam1, encParam2));
HttpURLConnection conn = (HttpURLConnection)serviceURL.openConnection();
// check response code -- if OK, proceed with processing output, otherwise handle errors
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream response = conn.getInputStream();
// read from the input stream and process results appropriately
} else {
// check error code and take appropriate actions
}
conn.close();
}
I left out the exception handling, but you'll need to account for those as well. Mostly subclasses of IOException, but there are some others as well (MalformedURLException for one). Hope that helps!
ASKER
oh here is my java code
just thought i would share the java code as well
thanks in advance for your help!
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
/**
* Send a POST request using URLConnection
* (Consider using HttpURLConnection or HttpsURLConnection)
*/
/**
*
* @author theodore werntz ii
*/
public class WebClientPHP {
public static void main(String[] args) {
try {
// Prepare data
String data = "testData" ; // grab users session id
// Prepare connection
URL url = new URL("http://127.0.0.1/subtest.php?name="+ data );
URLConnection connection = url.openConnection();
connection.setDoOutput(true); // Needed to write to a URLConnection
// Get the response
BufferedReader reader = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
// Print the response
String line;
while ((line = reader.readLine()) != null)
System.out.println(line);
reader.close(); // Close the reader
}
catch (Exception e) {
e.printStackTrace();
}
}
}
just thought i would share the java code as well
thanks in advance for your help!
Make sure you URLEncode the parameter string, just in case there are any reserved characters. Unless you are planning on pushing data to the connection, there's no need to call connection.setDoOutput(tru e). That would only be necessary if you were calling connection.getOutputStream () and writing data to it.
ASKER
can i have multiple strings i.e.
String data = <?php $_SESSION['UserName'] ?>;
String UserId = <?PHP $_SESSION['UserId'] ?>;
i will encode the params just gave that as an example.
then i will reference them i.e.
URL url = new URL("http://localhost/subtest.php?name=" + data + "&id=" +UserId);
thanks in advance for your help!
String data = <?php $_SESSION['UserName'] ?>;
String UserId = <?PHP $_SESSION['UserId'] ?>;
i will encode the params just gave that as an example.
then i will reference them i.e.
URL url = new URL("http://localhost/subtest.php?name=" + data + "&id=" +UserId);
thanks in advance for your help!
Yep. That should work. Especially with multiple parameters, I'd recommend going the String.format() route for the URL. Easier to read than String concatenation and more efficient as well.
URL url = new URL(String.format("http://localhost/subtest.php?name=%s&id=%s", data, UserId);
Oops... forgot the closing ) above.
ASKER
thanks in advance for your help!