PHP to Java

Posted on 2003-11-19
Last Modified: 2006-11-17

  We have a credit card interface written in PHP and our cc processor written in Java. What I want is to read the data from the PHP interface, pass it onto the Java processor and then readthe result back.

  The first solution I thought of is to use HTTP. Convert the java application into a Servlet and have the PHP redirect the flow to the Servlet. This is a possible solution but I am wondering how I could read the result back from the servlet (mind you I haven't got a clue about PHP syntax in order to read the input stream).

  The second solution is to use the Java extensions that come with PHP4. The problem here is that the live server is not configured with the Java extensions and I do not know how feasible it would be to take it down, recompile PHP with Java support and then run it.

  Also I am not sure how stable this Java extension is.

  Also, I found out that in order to do redirection to another page I have to use the header("Location: URL"); code. Does anyone knwo how I can pass variables along with it as well? Something similar to the following in Java

<jsp:forward page="myPage.jsp" >
   <jsp:param name="param1"
      value="value1" />

  Any more thoughts on the above two solutions? 500 points and an A answer for good opinions/information.
Question by:girionis
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
LVL 13

Expert Comment

ID: 9779244

you can use header("Location: page.jsp?variable1=value1&variable2=value2"); but i suppose you'd want it to be a bit more secure. if you wanted to use the post method then you could use this function: this could be used to essentially forward the form's values directly to java so it would be like posting the form to a java page after you've performed your php functions on the data. regarding java extensions, from the manual:

This extension is EXPERIMENTAL. The behaviour of this extension -- including the names of its functions and anything else documented about this extension -- may change without notice in a future release of PHP. Use this extension at your own risk.
having said that it might be feasible to take down php, add the option to include the extension and restart it. that's all i can think of for now


Expert Comment

ID: 9782693
The java extension is experimental. You could however add java functionality to PHP, using the Extension Mechanism.

That means you compile the java extension as an external module, and add it in using the extensions area of the php.ini file.

If you are using the apache module, on Linux, taking the server down and putting it back up with Java Support(after you've compiled the extension, and added it to php.ini), is simply a case of running 'apachectl restart'.

Expert Comment

ID: 9783868
Sorry, didn't read the question properly, You couldn't get the java extension installed. TBH, if you can't get the extension installed, you aren't very likely to get PHP installed with the java extension either.

One method you could use would be to store the information locally(either in files, or a database), that you put them into through PHP, and take them out through Java/JDBC.
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

LVL 35

Author Comment

ID: 9785402
 Thank you both for your replies.


> You couldn't get the java extension installed.

  Why I couldn't get the java extension installed? I haven't tried it yet, I am just asking for opinions :)

  As for the DB/local file suggestion, I do not think it's a right idea. I am not familiar with PHP and its syntax in general. Also I will need to notify PHP somehow for the result (i.e. if the cc is accepted or rejected) that's why I thought about the Java extension.

  I will leave this question open for now for any more suggestions :)
LVL 13

Expert Comment

ID: 9786668
when sending the result of the cc transaction, you can simply use variables in the url query string as i said - phppage.php?jspvariable=ccaccepted. this would be access in php as the variable $_GET["jspvariable"] (e.g. if($_GET["jspvariable"] == "ccaccepted") { print "transaction successful!"; } else { print "transaction failed."; }

as i said, if you want to keep all the variables integrated within the system rather than exporting them externally then bringing them back in, the function i provided above may be the one to use. using a database/flat file system would be feasible in my opinion as long as you could use a database which was easy to access from both php and java

LVL 35

Author Comment

ID: 9786715
 Yes but I need to send the request from PHP to a Servlet/JSP page and not another PHP. The problem is not how to send the request (I can simply redirect the flow) but how I can read the result back from the JSP page? The only thing I can think of is to read the request's input stream.

  For example, lets say that I have a PHP file with the following:

header("Location: page.jsp?variable1=hello");

  Then when I run the above and the flow is redirected to the page.jsp in the page.jsp I have smth like:

os = response.getOutputStream();
if (request.getParameter(variable1).equals("hello"))
   os.println("hello to you too");
    os.println("another hello");

  Now how should I read the string "hello to you too" that comes from the JSP page for example? Once the request is forwarded is there any way I can get back to it?

Accepted Solution

arjendv earned 500 total points
ID: 9796476
You should use the curl library, it anables you to post data to your jsp script, and get the results back in php.
There are a lot of examples in the user comments on  .
Using curl, you will also be able to post data via https to make it more secure.
Im sorry that i dont have the time to post a whole solution, but im sure this will point you in the right direction.
Using redirects will never be secure,and may cause potetial problems if users press the back and forward buttons in their  browser.


LVL 35

Author Comment

ID: 9802997
 Thank you for your answers. I am currently evaluating all of the suggestions, mainly the Java extension and the curl option. I will get back to you when I have some more info :)

  Meanwhile any more suggestions/info/ideas are more than welcome.
LVL 35

Author Comment

ID: 9830380
 Thank you, I finally used cURL and I found it easier to install and also use.

  Also thanks to the other two guys/gals who have helped me. I will post two separate questions for you so you can also get some points for your help and suggestions :)

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question