如何使用 PHP 读取 servlet 的响应

发布于 2024-11-09 18:42:16 字数 117 浏览 1 评论 0原文

我想从 PHP 向 Java Servlet 发送请求并接收来自该 Servlet 的响应并将其显示在 PHP 页面上。这应该怎么做呢?

感谢和问候

Abishek R Srikaanth

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北方的巷 2024-11-16 18:42:16

如果您想要的只是将 GET 请求的响应打印到 PHP 响应中,那么您可以使用 file_get_contents() 为此。

<?php echo file_get_contents("http://example.com/someservlet"); ?>

servlet 的 doGet() 方法将被调用,并且它返回的任何响应(甚至可以是转发的 JSP)都将作为字符串打印到 PHP 响应。

如果您想要更细粒度的控制,例如使用 POST 或其他东西,请前往 curl() 相反。链接的 PHP 手册包含几个示例。

无论采用哪种方式,请注意,每当它返回 HTML 时,您都应该确保最终得到有效的 HTML。例如,嵌套 标签是非法的。如果您不确定,请通过 w3 验证器传递 PHP 页面。否则,您最好必须解析 HTML 以提取感兴趣的 片段或使用 代替。

<iframe src="http://example.com/someservlet"></iframe>

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.

<?php echo file_get_contents("http://example.com/someservlet"); ?>

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.

<iframe src="http://example.com/someservlet"></iframe>
花辞树 2024-11-16 18:42:16

如果我理解正确的话,你想读取 php 中 servlet 的响应,然后从 php 输出它?

您可以使用 file_get_contents 到网址(可能不是最好的方法,但为了简单起见,它是最简单的),然后只是回显输出。

例如:

$content = file_get_content('http://www.google.com');
echo $content;

但是如果您希望能够在 servlet 端登录或使用会话,您将需要考虑其他事情。由于 php 向服务器发出的每个请求都是一个新请求,因此它不会像浏览器那样存储 cookie 等。

希望有帮助

If I'm understanding you correctly you want to read the response of a servlet in php and then output it from php?

You can use file_get_contents to the url (Probaly not the best way, but for simplicities sake it the easiest) and then just echo the output.

For example:

$content = file_get_content('http://www.google.com');
echo $content;

But if you want to be able to login or use the session at the servlet side you will need think of something else. As each request to the server from php will be a new one, it does not store cookies etc.. like browsers do.

Hope that helps

旧时模样 2024-11-16 18:42:16

这种情况可能需要重新考虑,但是,如果没有其他选择...

如果有一种方法可以实际更新 portlet,那么我建议创建某种形式的服务调用 - SOAP、自定义 RPC等等——在Java方面。从技术上讲,这是最正确的做事方法。

如果失败,如果这是一个简单的 GET 请求,则使用 file_get_contents。

如果必须是 POST/PUT/DELETE,那么您可以使用 cURL 。 cURL 还具有能够处理模拟会话的优点,这意味着您可以模拟登录以及随后的操作(尽管并非没有一些困难)。

如果您没有 cURL 并且需要 POST/PUT/DELETE,则 streams 库也许能够满足您的需求。

如果您没有流库或 cURL 并且需要 POST/PUT/DELETE,那么有 其他方法 来实现这一点,但也许你真的应该重新考虑这种情况。

如果上述所有方法都不起作用,那么您将需要驯服幽灵狼。幽灵狼只害怕火。我无法再帮助你,但如果你掌握了幽魂狼,他会引导你。祝你好运。

This is a situation which might do well to be rethought, but, if there are no other options...

If there is a way to actually update the portlet, then I would recommend creating some form of service call -- SOAP, custom RPC, etc -- on the Java side. Technically this is the most correct way to do things.

Failing that, if this is a simple GET request, then use file_get_contents.

If it has to be a POST/PUT/DELETE, then you can use cURL. cURL also has the benefit of being able to handle simulated sessions, which means that you are then able to simulate a log in and actions following that (though not without some difficulty).

If you don't have cURL and you need to POST/PUT/DELETE, then the streams library might be able to give you what you need.

If you don't have the streams library or cURL and you need to POST/PUT/DELETE, then there are other means of accomplishing that, but maybe you should really re-rethink that situation.

If all of the above don't work, then you will need to tame the Spectral Wolf. The Spectral Wolf fears only fire. I can no longer help you, but if you master the Spectral Wolf, he will guide you. Godspeed.

宛菡 2024-11-16 18:42:16

如果您确实想要这样做,您可以创建一个 Java 应用程序,该应用程序采用参数来填充请求和响应对象、实例化 servlet、运行正确的方法、获取结果并显示它,然后调用此方法来自 PHP 的 Java 应用程序。

或者您可以使用实验和不推荐的PHP / Java集成模块。

If you really want to do that, you can create a Java app that takes parameters to populate request and response objects, instantiates a servlet, runs the correct method, gets the result and displays it, then call this Java app from PHP.

Or you can use the experimental and unrecommended PHP / Java Integration module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文