Latest Post

Error: No input file specified.

While trying to run a codeigniter app the other day I came across this error just after I had finished uploading the files and making the ne...

Friday, October 21, 2011

Calling a php script from within java

Recently i worked on a project that required me to modify an existing java app inline with changes in the API through which the java system communicated with a service provider's business logic.

After weeks of trying to get the Java to communicate with the new web service, and not wanting to change anything else inside the java program except the web service client bit, which i could get to work (due to issues i did not have time to further investigate :)). I therefore decided to write the client in PHP, using NuSoap, and then simply call the NuSoap client from within java which worked out much easier than i expected.

All i needed from the client was the response XML, which would then be handled using preexisting java code so below is extracts from the java program.

[CODE]
try
{
URL phpClient = new URL("my php url here");


try{

URLConnection yc = phpClient.openConnection();

BufferedReader in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));

String inputLine;
Integer count = 1;
while ((inputLine = in.readLine()) != null) {
this.result += inputLine;
}

in.close();
} catch(IOException e){
System.out.println(" web php/service error " + e.toString());
}
} catch(MalformedURLException nameOfTheException){
//
}
[/CODE]

Chitika Ads