I would like to send a request to a Java Servlet from PHP and receive the response from the same and show it on the PHP page. How should this be done?
Thanks and Regards
Abishek R Srikaanth
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If all you want is to print the response of a GET request to an external resource plain vanilla into the PHP response, then you can use
file_get_contents()for that.The servlet’s
doGet()method will be invoked and whatever response it returns (which can even be a forwarded JSP) will be printed as string to the PHP response.If you want a little more fine grained control, e.g. using POST or something, then head to
curl()instead. The linked PHP manual contains several examples.Regardless of the way, please note that whenever it returns HTML, that you should ensure that you end up with valid HTML. For example, nesting
<html>tags is illegal. Pass the PHP page through the w3 validator if you’re unsure. Otherwise you’d better have to parse the HTML to extract the<body>pieces of interest or to use an<iframe>instead.